Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initialize C++ object member variables in the constructor?

I've got a class that has a couple of objects as member variables. I don't want the constructors for these members 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.

I thought maybe I could do the following, where the constructor is called immediately when initializing the object member variable:

class MyClass {     public:         MyClass(int n);     private:         AnotherClass another(100); // Construct AnotherClass right away! }; 

But I want the MyClass constructor to call the AnotherClass constructor. Here's what my code looks like:

FIle BigMommaClass.h

#include "ThingOne.h" #include "ThingTwo.h"  class BigMommaClass {          public:                 BigMommaClass(int numba1, int numba2);          private:                 ThingOne* ThingOne;                 ThingTwo* ThingTwo; }; 

FIle 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?

like image 432
Logical Fallacy Avatar asked Oct 17 '12 04:10

Logical Fallacy


People also ask

How do you initialize an object in a constructor?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

How do you initialize a variable in a constructor?

Using an initialization list is almost identical to doing direct initialization or uniform initialization. The member initializer list is inserted after the constructor parameters. It begins with a colon (:), and then lists each variable to initialize along with the value for that variable separated by a comma.

How do you initialize an object variable?

Type the keyword With , followed by an initialization list in braces. In the initialization list, include each property that you want to initialize and assign an initial value to it. The name of the property is preceded by a period. You can initialize one or more members of the class.

Which constructor is used for initializing an object through another object?

A copy constructor is a member function that initializes an object using another object of the same class.


1 Answers

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) {} 
like image 184
chris Avatar answered Sep 18 '22 08:09

chris