Copy Creator Question!

Asked 2 years ago, Updated 2 years ago, 24 views

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++

2022-09-22 18:19

1 Answers

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() {}
}


2022-09-22 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.