Answer the question
In order to leave comments, you need to log in
Why can't a constructor be created?
There are two classes, for example A and B. One class calls the constructor of the other.
Example:
class A {
A() {
(new B());
}
}
Answer the question
In order to leave comments, you need to log in
(new B());
"Wonderful" code. Only before spreading, it would be good to make sure that this error is reproduced on it.
Make the smallest possible code where you would have a problem that you are asking for help solving. It is likely that in the process of creating one you will suddenly find your mistake (maybe you, for example, messed up the call stack somewhere before calling the constructor).
And if you make a simple example where an error occurs, it doesn’t work - then you don’t understand correctly where it occurs and it’s probably somewhere in the “heap” that cannot be thrown out of the example.
Try like this
class A {
public:
A() {}
static A* create(A* val = nullptr) {
if (val == nullptr)
val = new A();
new B;
return val;
}
};
...
A* a = A::create();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question