Thread: Call contructor from constructor

  1. #1
    spaghetticode
    Guest

    Call contructor from constructor

    Hi there,

    I am currently working on classes and constructors. I have just worked on standard constructors and simple parameter constructors, and since I have been playing around with Java a bit in the past, a question came to my mind.

    My textbook uses a class "Place" with two integer coordinates as members as an example, and so far I have been writing two constructors:

    Code:
    class Place {
        public:
            Place();
            Place(int x, int y);
        ...
        private:
            int xCoord;
            int yCoord;
    }
    Code:
    Place::Place() {
        xCoord = 0;
        yCoord = 0;
    }
    
    Place::Place(int x, int y) {
        xCoord = x;
        yCoord = y;
    }
    Now my question is, in Java it is common practice and also considered good practice to redirect constructors. In C++, is there any disadvantage with this:

    Code:
    Place::Place() {
        Place(0, 0);
    }
    
    Place::Place(int x, int y) {
        xCoord = x;
        yCoord = y;
    }
    Under the hood, does this piece of code the same as the above? Is it considered good or bad practice? Is it something common in C++?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Unless you're using a C++11 compliant compiler you can't call one constructor from another constructor. Also even with C++11 you should be using initialization lists instead. For the simple example you have shown I wouldn't recommend calling another constructor from your constructor:

    Code:
    Place::Place() : xCoord(0), yCoord()
    {
    }
     
    Place::Place(int x, int y) : xCoord(x), yCoord(y)
    {
    }
    Jim

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, there is a great disadvantage: you are not delegating the constructor. Rather, you are creating a temporary object of the Place class within the default constructor. This object is destroyed as soon as it is created. If you want to delegate the constructor, then use the initialisation list, e.g.,
    Code:
    Place::Place() : Place(0, 0) {}
     
    Place::Place(int x, int y) : xCoord(x), yCoord(y) {}
    However, note that constructor delegation is a C++11 feature, so it is possible that your compiler does not support it yet.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    spaghetticode
    Guest
    Thanks to both of you, that's exactly what I was asking for. laserlight, I almost suspected something like this, thanks for the explanation. My compiler supports C++11 to a wide extend, and initialization lists are the next topic in my textbook.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-23-2012, 10:47 PM
  2. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  3. Call constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 02:16 AM
  4. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  5. Call constructor within constructor. How to?
    By xErath in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 05:30 PM