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

Joined
Jun 29, 2022
Messages
28
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);
    }
}
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top