I've got a class that has a couple of objects as member variables. I don't want the constructors for the objects to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing. o_O
我有一个类,它有几个对象作为成员变量。我不希望在声明的时候调用对象的构造函数,所以我试图显式地挂在指向对象的指针上。我不知道我在做什么。o_O
On *, I seem to be able to find other examples of object member variables, but usually the constructor is called immediately, like this:
在*上,我似乎可以找到其他对象成员变量的例子,但是通常会立即调用构造函数,如下所示:
class MyClass {
public:
MyClass(int n);
private:
AnotherClass another(100); // this constructs AnotherClass right away!
};
But I want the MyClass constructor to call the AnotherClass constructor. Here's what my code looks like:
但我希望MyClass构造函数调用另一个类构造函数。我的代码是这样的:
BigMommaClass.h
BigMommaClass.h
#include "ThingOne.h"
#include "ThingTwo.h"
class BigMommaClass {
public:
BigMommaClass(int numba1, int numba2);
private:
ThingOne* ThingOne;
ThingTwo* ThingTwo;
};
BigMommaClass.cpp
BigMommaClass.cpp
#include "BigMommaClass.h"
BigMommaClass::BigMommaClass(int numba1, int numba2) {
this->ThingOne = ThingOne(100);
this->ThingTwo = ThingTwo(numba1, numba2);
}
Here's the error I'm getting when I try to compile:
这里是我试图编译时的错误:
g++ -Wall -c -Iclasses -o objects/BigMommaClass.o classes/BigMommaClass.cpp
In file included from classes/BigMommaClass.cpp:1:0:
classes/BigMommaClass.h:12:8: error: declaration of âThingTwo* BigMommaClass::ThingTwoâ
classes/ThingTwo.h:1:11: error: changes meaning of âThingTwoâ from âclass ThingTwoâ
classes/BigMommaClass.cpp: In constructor âBigMommaClass::BigMommaClass(int, int)â:
classes/BigMommaClass.cpp:4:30: error: cannot convert âThingOneâ to âThingOne*â in assignment
classes/BigMommaClass.cpp:5:37: error: â((BigMommaClass*)this)->BigMommaClass::ThingTwoâ cannot be used as a function
make: *** [BigMommaClass.o] Error 1
Am I using the right approach but the wrong syntax? Or should I be coming at this from a different direction?
我使用的是正确的方法而不是错误的语法吗?或者我应该从另一个角度来看待这个问题吗?
3 个解决方案
#1
58
You can specify how to initialize members in the member initializer list:
您可以指定如何初始化成员列表中的成员:
BigMommaClass {
BigMommaClass(int, int);
private:
ThingOne thingOne;
ThingTwo thingTwo;
};
BigMommaClass::BigMommaClass(int numba1, int numba2)
: thingOne(numba1 + numba2), thingTwo(numba1, numba2) {}
#2
21
You're trying to create a ThingOne
by using operator=
which isn't going to work (incorrect syntax). Also, you're using a class name as a variable name, that is, ThingOne* ThingOne
. Firstly, let's fix the variable names:
另外,您将类名用作变量名,即ThingOne* ThingOne。首先,我们来确定变量名:
private:
ThingOne* t1;
ThingTwo* t2;
Since these are pointers, they must point to something. If the object hasn't been constructed yet, you'll need to do so explicitly with new in your BigMommaClass
constructor:
因为它们是指针,所以它们必须指向某个东西。如果对象还没有构建,那么您需要在BigMommaClass构造函数中显式地使用new:
BigMommaClass::BigMommaClass(int n1, int n2)
{
t1 = new ThingOne(100);
t2 = new ThingTwo(n1, n2);
}
Generally initializer lists are preferred for construction however, so it will look like:
然而,通常构造者列表是首选的,因此它将看起来如下:
BigMommaClass::BigMommaClass(int n1, int n2)
: t1(new ThingOne(100)), t2(new ThingTwo(n1, n2))
{ }
#3
7
This question is a bit old, but here's another way in c++11 of "doing more work" in the constructor before initialising your member variables:
这个问题有点过时了,但是在初始化成员变量之前,在c++11中还有一种方法,在构造函数中“做更多的工作”:
BigMommaClass::BigMommaClass(int numba1, int numba2)
: thingOne([](int n1, int n2){return n1+n2;}(numba1,numba2),
thingTwo(numba1, numba2) {}
The lambda function above will be invoked and the result passed to thingOnes constructor. You can of course make the lambda as complex as you like.
上面的lambda函数将被调用,结果传递给thingOnes构造函数。当然,你可以让lambda变得复杂。
#1
58
You can specify how to initialize members in the member initializer list:
您可以指定如何初始化成员列表中的成员:
BigMommaClass {
BigMommaClass(int, int);
private:
ThingOne thingOne;
ThingTwo thingTwo;
};
BigMommaClass::BigMommaClass(int numba1, int numba2)
: thingOne(numba1 + numba2), thingTwo(numba1, numba2) {}
#2
21
You're trying to create a ThingOne
by using operator=
which isn't going to work (incorrect syntax). Also, you're using a class name as a variable name, that is, ThingOne* ThingOne
. Firstly, let's fix the variable names:
另外,您将类名用作变量名,即ThingOne* ThingOne。首先,我们来确定变量名:
private:
ThingOne* t1;
ThingTwo* t2;
Since these are pointers, they must point to something. If the object hasn't been constructed yet, you'll need to do so explicitly with new in your BigMommaClass
constructor:
因为它们是指针,所以它们必须指向某个东西。如果对象还没有构建,那么您需要在BigMommaClass构造函数中显式地使用new:
BigMommaClass::BigMommaClass(int n1, int n2)
{
t1 = new ThingOne(100);
t2 = new ThingTwo(n1, n2);
}
Generally initializer lists are preferred for construction however, so it will look like:
然而,通常构造者列表是首选的,因此它将看起来如下:
BigMommaClass::BigMommaClass(int n1, int n2)
: t1(new ThingOne(100)), t2(new ThingTwo(n1, n2))
{ }
#3
7
This question is a bit old, but here's another way in c++11 of "doing more work" in the constructor before initialising your member variables:
这个问题有点过时了,但是在初始化成员变量之前,在c++11中还有一种方法,在构造函数中“做更多的工作”:
BigMommaClass::BigMommaClass(int numba1, int numba2)
: thingOne([](int n1, int n2){return n1+n2;}(numba1,numba2),
thingTwo(numba1, numba2) {}
The lambda function above will be invoked and the result passed to thingOnes constructor. You can of course make the lambda as complex as you like.
上面的lambda函数将被调用,结果传递给thingOnes构造函数。当然,你可以让lambda变得复杂。