Working in on in C++:
我有以下代码:
class Foo{
private:
string bars[];
public:
Foo(string initial_bars[]){
bars = initial_bars;
}
}
它不能编译并抛出以下错误:
error: incompatible types in assignment of 'std::string*' to 'std::string [0u]'
我注意到删除行bars = initial_bars;就解决了这个问题。看起来我
我想知道如何在c++的构造函数中创建指向新创建的对象的指针?
班级的地址是什么?
class MyClass
{
public:
};
class MyClass2
{
public:
//I need a pointer to the created object
MyClass2 *pObjectName;
//Constructor
MyClass2()
{
pObjectName = &//I have no clue how to get the adress of the (not yet) cre
我是C++中的工厂模式的新手,当我尝试实现一个方法时,我得到了标题,我得到了以下错误:
在静态成员函数中对成员'creationFunctions‘的使用无效
typedef Shape (*createShapeFunction)(void);
/* thrown when a shape cannot be read from a stream */
class WrongFormatException { };
class ShapeFactory {
public:
static void registerFunction(const std::string &a
我从c++ primer中了解到,我们可以通过以下方式使用类型定义:
typedef int mytype;
typedef int *mytype;
然而,下面的代码也可以编译,它似乎创建了一个新的类型" rt ",我很好奇rt的类型是什么,以及这种类型的typedef的常见用途是什么?
class A{
public:
int c;
};
typedef int A::* rt;
以下是从C++ Vol1第10章中的思考中复制的一些代码。
#include <iostream>
using namespace std;
int x = 100;
class WithStatic {
static int x;
static int y;
public:
void print() const {
cout << "WithStatic::x = " << x << endl;
早上好。我很难理解在共享项目中使用C++中的对象进行深度和浅层复制背后的逻辑,所以我创建了以下示例。
int main() {
ObjectAType* objecta = ObjectAType::New();
ObjectBType* objectb = ObjectBType::New();
// some operation to populate data members of object a
objecta->Operation();
// assume I have accessors to return datamembers
在下面的C++程序中,从函数修改静态数据成员const可以正常工作:
class A
{
public:
static int a; // static data member
void set() const
{
a = 10;
}
};
但是,从const函数修改非静态数据成员不起作用:
class A
{
public:
int a; // non-static data member
void set() const
{
a = 10;
}
};
为什么const
可能重复:
因此,我目前正在学习关于数据结构和算法开发的C++考试。尽管我的老师很有力量,但我注意到他经常使用这个"->“。我不知道这意味着什么?这真的是您可以在c++中执行的命令吗?
示例1
addrInfo *ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
// at this point, ptr points to the last item
示例2
if( head == NULL )
{
head = block;
block->next = N
我是c++新手,我比c++了解更多的python,我不得不将代码从c++更改为python,在要更改的代码中我找到了以下句子:
p->arity = std::stoi(x, nullptr, 10);
我想为了简单起见,我们可以用
p->arity = x; /* or some whit pointers im really noob on c++ but i think this is not importan */
在python中,这是否意味着
p[arity] = x
或者类似的?不然呢?我有点迷失了,所有这些新的(对我来说)概念,比如指针和内存之类的东西,但是现在
假设我有以下C++类:
class Foo
{
double bar(double sth);
};
double Foo::bar(double sth)
{
double a,b,c,d,e,f
a = b = c = d = e = f = 0;
/* do stuff with a..f and sth */
}
函数bar()将在一个循环中被调用数百万次。显然,每次调用它时,都必须分配变量a..f。通过将变量设置为Foo类的成员a..f并在函数的入口点对其进行初始化,是否可以获得任何性能?另一方面,通过这个->,a..f的值将被解除引用,所以我想知道这是否实际
我想知道在C++中是否可以将一个成员变量输入到对象指针中?
我有一堂课如下-
class Foo
{
private:
int x;
int y;
int z;
public
void func1(...);
void func2(...);
void func3(...);
}
此外,我还有一个第三方代码,它有一个回调作为-
void callbackFunction (int *xPointer)
{
// This is what I want to do
Foo * fooPtr = (Foo*)xPointer;