Constructor chaining in C++
Asked Answered
H

4

45

My understanding of constructor chaining is that , when there are more than one constructors in a class (overloaded constructors) , if one of them tries to call another constructor,then this process is called CONSTRUCTOR CHAINING , which is not supported in C++ . Recently I came across this paragraph while reading online material.... It goes like this ...

You may find yourself in the situation where you want to write a member function to re-initialize a class back to default values. Because you probably already have a constructor that does this, you may be tempted to try to call the constructor from your member function. As mentioned, chaining constructor calls are illegal in C++. You could copy the code from the constructor in your function, which would work, but lead to duplicate code. The best solution in this case is to move the code from the constructor to your new function, and have the constructor call your function to do the work of initializing the data.

Does a member function calling the constructor also come under constructor chaining ?? Please throw some light on this topic in C++ .

Hopfinger answered 8/9, 2011 at 13:54 Comment(5)
I'd say not, and it's a non-sequitur for the article to say that constructor chaining is illegal, immediately after saying that some arbitrary member function (not necessarily a constructor) might think it wants to call a constructor to reset the value. But who knows, you haven't cited the article or quoted where it describes constructor chaining, so maybe it's using it in some unusual sense already described elsewhere.Carotenoid
It's not illegal. Calling a constructor from a member has the effect of creating a temporary object to which the construction applies, not the 'this' object from which you make the call.Consumer
@Amardeep: agreed, it would be more accurate to say that constructor chaining is "impossible" in C++03, rather than "illegal". There's simply no syntax to describe doing it. As you say, by the normal definition calling a constructor isn't at all the same thing as constructor chaining.Carotenoid
@ Steve .. and everyone else intrested .. Here's the link :)Hopfinger
C++ constructor call another constructor is a feature in c++ 11, called Delegating Constructors, see link open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdfZorine
S
21

The paragraph basically says this:

class X
{
   void Init(params) {/*common initing code here*/ }
   X(params1) { Init(someParams); /*custom code*/ } 
   X(params2) { Init(someOtherParams); /*custom code*/ } 
};

You cannot call a constructor from a member function either. It may seem to you that you've done it, but that's an illusion:

class X
{
public:
    X(int i):i(i){}
    void f()
    {
       X(3); //this just creates a temprorary - doesn't call the ctor on this instance
    }
    int i;
};

int main()
{
    using std::cout;
    X x(4);
    cout << x.i << "\n"; //prints 4
    x.f();
    cout << x.i << "\n"; //prints 4 again
}
Synonymous answered 8/9, 2011 at 14:1 Comment(1)
@ Armen .. You mean the first time it is printing a 4 , it is actually calling the constructor , and second time when it prints 4 , though I feel as if I have acheived the result by calling the constructor( from the function void f() ), what EXACTLY is happening is not that .. !!! Is it ?? But I didn't understand your first comment in the above code // this just creates a temporary - doesn't call the ctor on this instance . Could you explain it to me a bit more clearly ?Hopfinger
L
43

C++11 allows constructor chaining (partially). This feature is called "delegating constructors". So in C++11 you can do the following

class Foo
{
public:
    Foo(int a) : Foo() { _a = a; }
    Foo(char* b) : Foo() { _b = b; }
    Foo() { _c = 1.5; }
private:
    int _a = 0;
    char* _b = nullptr;
    double _c;
};

However, there is a severe limitation that a constructor that calls another constructor is not allowed to initialize any other members. So you cannot do the following with a delegating constructor:

class Foo
{
public:
    Foo(int a) : Foo(), _a(a) { }
    Foo(char* b) : Foo(), _b(b) { }
    Foo() { _c = 1.5; }
private:
    int _a = 0;
    char* _b = nullptr;
    double _c;
};

MSVC++2013 gives compile error "C3511: a call to a delegating constructor shall be the only member-initializer" for the latter code example.

Lumber answered 22/10, 2015 at 7:10 Comment(2)
I guess that makes sense. Calling the default constructor should initialize all fields, so initializing them again could be very bad.Babbler
If you feel you need to do this, it suggests that there's a small parent class trying to get out of Foo. In this case, FooParent would have the member c, and FooChild would inherit from it and add a and b.Burdensome
S
21

The paragraph basically says this:

class X
{
   void Init(params) {/*common initing code here*/ }
   X(params1) { Init(someParams); /*custom code*/ } 
   X(params2) { Init(someOtherParams); /*custom code*/ } 
};

You cannot call a constructor from a member function either. It may seem to you that you've done it, but that's an illusion:

class X
{
public:
    X(int i):i(i){}
    void f()
    {
       X(3); //this just creates a temprorary - doesn't call the ctor on this instance
    }
    int i;
};

int main()
{
    using std::cout;
    X x(4);
    cout << x.i << "\n"; //prints 4
    x.f();
    cout << x.i << "\n"; //prints 4 again
}
Synonymous answered 8/9, 2011 at 14:1 Comment(1)
@ Armen .. You mean the first time it is printing a 4 , it is actually calling the constructor , and second time when it prints 4 , though I feel as if I have acheived the result by calling the constructor( from the function void f() ), what EXACTLY is happening is not that .. !!! Is it ?? But I didn't understand your first comment in the above code // this just creates a temporary - doesn't call the ctor on this instance . Could you explain it to me a bit more clearly ?Hopfinger
G
4

That's not what the text says. It's suggesting your constructor call a member function which is normal and legal. This is to avoid explicitly calling the ctor again and to avoid duplicating code between your ctor and reset function.

Foo::Foo() {
  Init();
}

void Foo::Reset() {
  Init();
}

void Foo::Init() {
  // ... do stuff ...
}
Galenism answered 8/9, 2011 at 13:58 Comment(4)
It's true that the text is not recommending that the function call the constructor, however the text says "you may be tempted to try to call the constructor from your member function. As mentioned, chaining constructor calls are illegal in C++". This suggests that via some confusion of ideas, the author thought that "chaining constructor calls" has something to do with "calling the constructor from your member function", regardless of whether the article is recommending doing it or not. Could just be dodgy editing, of course.Carotenoid
"Because you probably already have a constructor that does this, you may be tempted to try to call the constructor from your member function." I thought this meant that A MEMBER FUNCTION CALLING A CONSTRUCTOR should be avoided .. !!???Hopfinger
@Appy: not necessarily. For example, in a class called Foo *this = Foo(); might be a perfectly reasonable way to reset the object, if not necessarily the most efficient. Then the function calls the constructor, it just doesn't call it to construct this.Carotenoid
@ Steve ... Thanks for the help .. :) I guess it IS just some kinda dodgy editing ..Hopfinger
A
-2

when we call constructor from a member function, then it will temporary create a object of its type. in case if we are calling in derived class function then all the parent constructors are also gets executed and destroyed using destructor once function goes out of scope.

its not a Good Practice to call the constructors in member functions since it creates objects of every class derived.

Asphaltite answered 16/11, 2013 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.