constructor overloading question

This is a discussion on constructor overloading question within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been working with Java for a while and had minimal c++ experience before, although a lot of c ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    constructor overloading question

    Hi,

    I've been working with Java for a while and had minimal c++ experience before, although a lot of c experience. So most of the problems I'm running into have to do with the way c++ handles objects since it is a bit different than Java. On to the question...

    I know how to overload constructors and I can use them like so:

    Code:
    Rectangle::Rectangle(void) {
    	this->theta = 0;
    	this->validateRotation();
    }
    
    Rectangle::Rectangle(int width, int height) {
    	this->width = width;
    	this->height= height;
    	this->theta = 0;
    	this->validateRotation();
    }
    
    Rectangle::Rectangle(int width, int height, Point2D center) {
    	this->width = width;
    	this->height= height;
    	this->center = center;	
    	this->theta = 0;
    	this->validateRotation();
    }
    As yo can see there's a lot of repetition there. Is there a way to call other constructors from within a constructor? Like...

    Code:
    Rectangle::Rectangle(void) {
    	this->theta = 0;
    	this->validateRotation();
    }
    
    Rectangle::Rectangle(int width, int height) : Rectangle() {
    	this->width = width;
    	this->height= height;
    }
    
    Rectangle::Rectangle(int width, int height, Point2D center) : Rectangle(width,height) {
    	this->center = center;	
    }
    I tried that but it doesn't compile, I figured this may work since I have a subclass of Rectangle where I can do that (and it compiles fine). Is it possible to do this?

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is there a way to call other constructors from within a constructor?
    No. Constructors are only invoked when the object is created. However you can speed things up slightly by using initialization lists:
    Code:
    Rectangle::Rectangle(int width, int height, Point2D center): width(width), height(height), center(center)
    {
    	this->theta = 0;
    	this->validateRotation();
    }

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, you could use default arguments to help in this case.

    Code:
    Rectangle::Rectangle(int width = 0, int height = 0, Point2D center = center())
    {
    	this->width = width;
    	this->height= height;
    	this->center = center;	
    	this->theta = 0;
    	this->validateRotation();
    }
    And then call your constructor in all three ways you specified.

    Code:
    Rectangle r1 = Rectangle();
    Rectangle r2 = Rectangle(5, 10);
    rectangle r3 = Rectangle(5, 10, Point2D(blah));

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And you could include theta in the initialization list too:
    Code:
    Rectangle::Rectangle(int width, int height, Point2D center): width(width), height(height), center(center), theta(0)
    {
    	validateRotation();
    }

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    And if ya really wanted to, you could do the whole damned buhjeezleshmittengoop.

    Code:
    Rectangle::Rectangle(int width = 0, int height = 0, Point2D center = 0, int theta = 0)
            : width(width), height(height), center(center), theta(theta)
    {
    	validateRotation();
    }

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    interesting, I like the last suggestion, I think I may try that,

    Thanks for the input

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yoo seee? He like tha buhjeezleshmittengoop. Oo ya.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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