Thread: Constructor for a class

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    10

    Constructor for a class

    I am current trying to learn C++ and I'm a little confused about a constructor I found in a book I am reading and was wondering if anyone can explain to me what something means

    Code:
    //this is the constructor
    objectc::objectc(int x, int y):
    itsx(x),     //Book says these are initializations
    itsy(y)
    {}  //empty body
    Ok... for the class objectc, we have this constructor, right? What are those initializations? I look in the book everywhere and wasn't able to find anything about them. It appears its taking the value in x and assigning it to itsx which is declared as a private variable in the class but the syntax looks like its calling a function, kinda... hence my confused state.

    Can someone explained to me what these initializations are and what is the syntax to use them and what else can be used in this area?
    Between the constructor
    objectc::objectc(int x, int y):
    and the body
    {}

    Thanks in Advance.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    They are used to initialize members of the class They don't get real important until after you have classes that contain other classes as instances. In short, they prevent you from constucting an object and then using assignment, in favor of just directly constructing an object.

    Code:
    className::className(type1 arg1, type2 arg2, type3 arg3) :
    member1(arg1, arg2),
    member2(arg3)
    { }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    well .........

    The class apparently has two members of type integer, itsx and itsy. The class is using a constructor known as an initializer list (or sometimes other names like member-initialization list according to one book I have). The value of the arguments x and y, are assigned to the members itsx and itsy respectively. It's just not occurring in the body of the constructor.

    Most books I have read say that this is the preferred method because when using this type of constructor, the compiler actually assigns the values to the members before the constructor begins to execute (EDIT* IS WHAT I MEANT TO SAY). That's about the best explanation I can give.

    Are itsx and itsy const member data? Because that would be a reason to use this type of constructor.
    Last edited by DISGUISED; 07-25-2002 at 06:23 PM.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    10
    Thanks for the explaination, guys. I think I have an understanding of what they are enough to get me pass this bit of code.

    They aren't const data members, DISGUISED. They are only private members.


    I'm not sure why this book decided to throw me a curve ball like that. Using something in example code that they never explained.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Here is something you can try that is kind of interesting, and along the same lines as Constructor Initializer Lists...

    float f = 3.14159; // usual way

    and...

    float f(3.14159); // unusual !

    mean exactly the same thing, assign the value 3.14159 to the float primative type. The C++ compiler allows primative types to be initialized as though they were a user defined type with a constructor call.

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    In some sense it is just a syntax that you can use when declaring a constructor in a header .h:

    Otherwise you would do this in a source class implementation file .c
    Code:
    Your implementation source file:
    objectc::objectc(int x, int y)
    {
    itsx = x;
    itxy = y;
    }
    
    And have this in your header:
    objectc::objectc(int, int);
    I think this is true anyway. To be honest I sort of forget the answer Mr. H. I will come across this in my books in about six weeks from now though. It will refresh my memory.
    Last edited by Troll_King; 08-06-2002 at 08:11 AM.

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by SilentStrike
    They are used to initialize members of the class They don't get real important until after you have classes that contain other classes as instances. In short, they prevent you from constucting an object and then using assignment, in favor of just directly constructing an object.

    Code:
    className::className(type1 arg1, type2 arg2, type3 arg3) :
    member1(arg1, arg2),
    member2(arg3)
    { }

    This format for initialisation has to be used for constant data members of the class. Assignment is not permitted like normal data members.
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM