Like Tree2Likes
  • 1 Post By jimblumberg
  • 1 Post By laserlight

Call contructor from constructor

This is a discussion on Call contructor from constructor within the C++ Programming forums, part of the General Programming Boards category; Hi there, I am currently working on classes and constructors. I have just worked on standard constructors and simple parameter ...

  1. #1
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197

    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++?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    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
    dennis.cpp likes this.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    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.
    dennis.cpp likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

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, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21