Thread: C++ have a constructor call another constructor

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    C++ have a constructor call another constructor

    So... I understand why this doesn't work, I just can't think a way to do it (besides copy and pasting from the first constructor to the second, which I want to avoid).
    Code:
    class Foo {
       // The REAL constructor
       Foo (Bar const & B) { 
          // Complicated stuff
       }
    
       // A constructor that turns i into a Bar so we can call the real constructor.
       Foo (int i) {
          Bar converted_i;
          // Complicated stuff that turns i into a Bar.
          Foo (converted_i);
       }
    
       // ...
    };
    This doesn't work as I had hoped. Instead of having the int constructor 'switch over' to the Bar constructor, it just constructs a temporary Foo object, which does nothing.

    I hope the comments make it clear what I'm trying to do... but for the life of me, I don't know how.
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    Yeah, I think I get it. First of all, from a design standpoint, Foo's constructor shouldn't know how to convert an integer to a Bar. One of Bar's constructors/methods should do this. What I would do is create a protected method(non-constructor) that turns this instance of Foo into the Bar given, however you do that. Then just call that in the two constructors. Hope this helps.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by UMR_Student View Post
    Yeah, I think I get it. First of all, from a design standpoint, Foo's constructor shouldn't know how to convert an integer to a Bar. One of Bar's constructors/methods should do this. What I would do is create a protected method(non-constructor) that turns this instance of Foo into the Bar given, however you do that. Then just call that in the two constructors. Hope this helps.
    I understand what you are saying, and the more I think about it, the more that's the solution. I just wished for something a bit more elegant.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by QuestionC View Post
    I understand what you are saying, and the more I think about it, the more that's the solution. I just wished for something a bit more elegant.
    Code:
    class Bar
    {
    public:
        Bar(int x);
    };
    
    class Foo {
    public:
       // The REAL constructor
       Foo (Bar const & B) { 
          // Complicated stuff
       }
    
       // ...
    };
    
    int main()
    {
        Foo x(10);
    }
    This works for me... Foo takes a Bar reference, and a Bar can be constructed from an int, so the compiler automatically constructs a Bar from the value 10.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Constructor chaining is not supported in C++03. You'll have to wait for C++09 for this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gcc can't find obvious copy constructor
    By SevenThunders in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2009, 02:41 PM
  2. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM