This question already has answers here:
How to initialize the reference member variable of a class?
(2 answers)
Closed 2 years ago.
I'm trying to pass a class reference to a field, but I get the error "'mazeGenerator::maze' references must be initialized".
I tried initializing 'maze' above the class constructor.
Why is this happening?
class mazeGenerator {
public:
Maze& maze;
mazeGenerator(Maze& mazeObj) {
maze=mazeObj;
}
}
You must initialize data members of reference type with a member initializer list, like this:
mazeGenerator(Maze& mazeObj) : maze(mazeObj) {}
This is also the case with const data members.
Note that putting the declaration of the data member above the constructor doesn't actually make any difference; it could be declared below as well.
Also, your data members (whether they are of reference type or not), should be private to the class.
Related
This question already has answers here:
Can I use C++ class members initialized in the initializer list, later in the list?
(2 answers)
Can member variables be used to initialize other members in an initialization list?
(5 answers)
Closed 3 years ago.
Let's say I have this constructor :
MyClass::MyClass()
: m_foo(new Foo())
, m_bar(new Bar(m_foo))
{
}
Is it safe/legit to use a member like this to initialize other members ?
Thanks
It depends on the order in which m_foo and m_bar appear in the class definition. Since data members are initialized in order of definition, the code above is safe only if m_foo appears before m_bar.
Compilers usually warn about order mismatches between the constructor initialization list and class definition.
This question already has an answer here:
How to initialize a shared_ptr that is a member of a class?
(1 answer)
Closed 8 years ago.
I have a class as:
class LargeObject
{
public:
LargeObject();
void DoSomething();
private:
std::unique_ptr<Thing> pThing;
};
Then when I want to create the pointer in the constructor
LargeObject()
{
pThing(new Thing()); //This does not work.
}
I want to use the member variable throughout the code. How to do that?
I think initialization should be in constructor's initialization list, that's the place where constructors should be invoked from another constructor:
LargeObject()
:pThing(new Thing){}
This question already has answers here:
How can I initialize base class member variables in derived class constructor?
(7 answers)
Closed 4 years ago.
I have a mistake I cannot understand when initializing member data in an inherited class with two different methods I thought they should be theoretically identical.
class gSolObject
{
public:
gSolObject();
virtual ~gSolObject(){}
bool isCollisionObject;
};
class gPlanetObject : public gSolObject
{
public:
gPlanetObject();
~gPlanetObject(){};
};
gSolObject::gSolObject():isCollisionObject(1)
{
}
gPlanetObject::gPlanetObject():gSolObject(),isCollisionObject(0)
{
}
I get an error class 'gPlanetObject' does not have any field named 'isCollisionObject'.
However when I put the initialization right into the constructor's brackts {..} instead:
gPlanetObject::gPlanetObject():gSolObject()
{
isCollisionObject=0;
}
It compiles fine. Why would that be?
EDIT: This also does not work
gPlanetObject::gPlanetObject():gSolObject(),gSolObject::isCollisionObject(0)
It writes 'expected class-name before '(' token'
You can't initialize member variables declared in base classes, because the base class constructor has already initialized them. All base constructors execute before member constructors.
You can reassign it. Or you can call a base class constructor that takes an argument and initializes its members with that value.
Edited : You can't call a method of a uninitialized object (here gSolObject) and that's why it works when you execute isCollisionObject(0) in the constructor. Furthermore, if you always set it to 0, then you should use default value in the gSolObject constructor.
This question already has answers here:
Reference as class member initialization
(4 answers)
Closed 9 years ago.
I am sorry if this meight be a really simple question but i am new to c++ and working on a simple vocable trainer for understanding c++. (come from java..)
I'd like to pass a const FileManager as reference to my Logic. But i don't get it work. I don't want to have a copy or such.
So i tried it like this: (the main)
FileManager& file = FileManager();
Logic logic = Logic(file);
Inside of the Logic i'd like to store the reference:
class Logic
{
public:
Logic(const FileManager& manager);
~Logic();
private:
const FileManager& m_fileManager;
};
Logic::Logic(const FileManager& manager) :
{
m_fileManager = manager;
}
Thanks
Once the body of a constructor is entered, all member variables have already been initialized. Thereafter, you can only assign to them. This can't work with references - as we know, they need to be initialized when their lifetime begins.
You need to use member initializer list:
Logic::Logic(const FileManager& manager)
: m_fileManager(manager) // m_fileManager is initialized here
{
}
Consider if you really want a reference member. For one, they make your class non-assignable. A smart pointer might be a better choice.
Your example has three flaws. The first, is that you try to initialize a none const reference with a temporary object:
FileManager& file = FileManager();
Most likely you just want to use a FileManager instance here:
FileManager file;
Second, references must be initialized. You achieve this by using the initializer list syntax:
Logic::Logic(const FileManager& manager)
: m_fileManager( manager )
{
}
In addition, the initializing, you use for Logic requires that Logic is assignable. Simply use:
Logic logic(file);
If you have reference members in a class, objects of that class are not assignable by default.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ initialization lists
class Base
{
public:
int m_nValue;
Base(int nValue=0)
: m_nValue(nValue)
{
}
};
In this code, is the constructor initializing m_nvalue member variable?
I am not sure of this syntax:
Base(int nValue=0) : m_nValue(nValue) {}
We normally write it as:
Base(int nValue) { m_nValue = nValue;}
Can some one explain the above syntax of C++?
This syntax:
Base(int nValue=0)
: m_nValue(nValue)
is called the member initializer. It will initialize m_nValue with given nValue. This syntax is usually preferred in C++ since it is executed before the body of the constructor.
It's called member initializer list.
The member initializer list consists of a comma-separated list of initializers preceded by a colon. It’s placed after the closing
parenthesis of the argument list and before the opening bracket of the function body
Conceptually, these initializations
take place when the object is created and before any code within the brackets is executed.
Note:
You can’t use the member initializer list syntax with class methods other than constructors.
The way of initializing a variable in your code is called as member initializer list.
Generally we use such list to initialize const member variable (normal - non const also) we because at the time of construction we can give some value to const variable.
Second type of Initialization is basically a normal Parametrised constructor. That is used when you are having a object and at the time of creation of object you want to initialize the member variable.