constructor

This is a discussion on constructor within the C++ Programming forums, part of the General Programming Boards category; Hi all! I'm writing a program in which I define a class named Point. the objects of the class are ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Question constructor

    Hi all!
    I'm writing a program in which I define a class named Point. the objects of the class are points of 2 numerical coordinates(x and y). I wanna write a constructor for my class with the following signature:
    Point(int x,int y)
    should I write anything in the body of my constructor or should I leave it empty?
    I mean sth like this:
    Code:
    class Point
    {
    public:
    int x,y;
    Point(int x,int y)
        {
          Point::x=x;   
          Point::y=y;
         }
    };
    is it ok to do so?
    I wanna tell the constructor to set the x and y attributes of the objects as written in the parenthesis that come after them. if I leave it's body empty, does it know to do such a thing?
    Last edited by Farnaz; 03-05-2011 at 07:36 AM.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    The best way to do this is via an initializer list:

    Code:
    Point(int x_, int y_) : x(x_), y(y_) {}
    note I changed the names of the variables coming in to set them apart from the member variables.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    If you're using initialization list syntax, the names of the arguments can be the same as the member names, and the compiler is smart enough to know what to do:

    Code:
    Point(int x, int y) : x(x), y(y) {}
    Code:
    Point(int x,int y)
        {
          Point::x=x;   
          Point::y=y;
         }
    };
    This would work if x and y were static, for non-static members, you could alternatively do this:

    Code:
    Point(int x,int y)
        {
          this->x=x;   
          this->y=y;
         }
    };
    The initialization list is the preferred way to do this in a constructor, but you can use this in any other place you need to differentiate a member variable from a local, for example.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Good to know, NeonBlack. I always use trailing underscores on my member variables for differentiation to prevent myself from getting confused anyway

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    Thank you all! it helped me a lot. but could you please explain a little bit more why my code works for static members and not non-static ones?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help - overloaded constructor taking char or int
    By DHart07 in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2010, 03:57 AM
  2. Replies: 1
    Last Post: 06-10-2008, 09:38 PM
  3. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 02:59 AM
  4. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  5. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM

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