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){}
Related
This question already has answers here:
Why can't member initializers use parentheses?
(2 answers)
How to create an object in a form like this: ifstream in();
(1 answer)
What are the advantages of list initialization (using curly braces)?
(5 answers)
Closed 5 months ago.
In a class, if I have:
private:
MyClass myObj;
vs
private:
MyClass myObj();
private:
MyClass myObj{};
And assuming MyClass takes no parameter in its constructor.
MyClass myObj;
This declares a class member named myObj, that gets default-constructed, by default.
MyClass myObj();
This declares a class method, a class function, named myObj that takes no parameters and returns a MyClass object.
MyClass myObj{};
This also declares a class member named myObj, that gets default-constructed, by default, just like without the {}.
Welcome to C++.
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.
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 answers here:
Does a constructor / destructor have to have code, or is the function enough?
(3 answers)
Closed 9 years ago.
I have confusion regarding default and empty constructor. Does empty constructor also initializes class variable automatically ? Meaning if i use a empty constructor instead of default constructor , does that also initialize class member variable automatically ? For example, if use following code, does integer pointer is initialized to NULL ? Please confirm
// .h file
Class Test {
public:
Test();
~Test();
int *p;
}
// .cpp file
Test::Test()
{
// do something..
}
No, empty constructor is same as default constructor if you don't initialize any member variable inside it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why should I prefer to use member initialization list?
Class A has a member variable i. i can be initialized or assigned during object creation.
A) Initialise
class A {
int i;
public:
A(int _i) : i(_i){}
}
B) assign
class A {
int i;
public:
A(int _i) : { i = _i}
}
My question is what is the basic difference between these 2 approach?
The difference lies in which C++ mechanism is used to initialize i in your class. Case (A) initializes it via constructor, and case (B) uses the assignment operator (or a copy constructor if no assignment operator is defined).
Most C++ compilers would generate exactly the same code for this particular example, because you're using int, which is a "plain old data" type. If i were a class type, it could make a great deal of difference.