class TestClass
{
int data;
TestClass() : data(2) {}
TestClass(const TestClass& tc) {}
}
For example, when you run the copy generator above<:/p>
I want to call the default generator without parameters to make data 2.
Is there any way to call the default constructor?
c++
Starting with C++11, the concept of Generator Delegation has been added.
You can call another constructor in the class to the initialization list of constructors, as shown below. However, in this case, in addition to calling other constructors, you cannot initialize member variables.
class TestClass
{
int data;
TestClass() : data(2) {}
TestClass(const TestClass& tc) : TestClass() {}
}
© 2024 OneMinuteCode. All rights reserved.