1
class draw
{
    draw(circle i)
    {
        // draw a circle;
    }

    draw(circle i, circle j)
    {
        draw(i);
        draw(j);
    }
}

Can I call another overloaded constructor of the same class in C++ even if using template?

0

1 Answer 1

2

No, in C++ you cannot have one constructor directly call another (well, at least not more than once). However, you can have each constructor call a third method that performs actual work.

class draw
{
    draw(circle i)
    {
        do_draw(i);
    }

    draw(circle i, circle j)
    {
        do_draw(i);
        do_draw(j);
    }

    void do_draw(circle c)
    {
        // draw a circle;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well didn't c++11 (or at least c++14) introduced calling other constructors in the member initializer list?
Yes, C++11 allows a constructor to call another constructor of the same class in the initializer list. It is called delegating constructors.
That's right, but you can only call a delegated constructor once, not more than once as this question suggests.
@GregHewgill How the question suggests, that the constructor should be called more than once? Can you elaborate about an actually applicable solution regarding the current standard please?
@πάνταῥεῖ: What? I'm not talking about any standard. I'm observing that the OP's two-argument constructor actually wants to draw two circles. If there is a single-argument constructor that draws one circle, you can't call that constructor twice to draw two different circles. One would have to create a third method as I have suggested in my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.