In C++, can I invoke a constructor from another constructor (constructor chaining)?


Joined
Jun 29, 2022
Messages
4
Reaction score
0
As a C# developer, I'm used to iterating through constructors: I've been reading this article online from scaler topics and I wanted to know, Is it possible to achieve this in C++? Both invoking the Class name and using the 'this' keyword fail.

Code:
class Test {
    public Test() {
        DoSomething();
    }

    public Test(int count) : this() {
        DoSomethingWithCount(count);
    }

    public Test(int count, string name) : this(count) {
        DoSomethingWithName(name);
    }
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top