Thread: What does this mean? Strange Konstruktor.

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    What does this mean? Strange Konstruktor.

    Hi!

    I'm a bit unsure about a konstruktor in a class deklaration.

    what does "hwColorType():r(),g(),b(){}" mean?

    Is it really needed?

    Code:
    class hwColorType {
    public:  
    	GLubyte r,g,bl;
    	hwColorType():r(),g(),b() {}
    	hwColorType(GLubyte rr, GLubyte gg, GLubyte bb):r(rr),g(gg),b(bb) {}
    	
    };

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    it is an initializer list; at which it isn't doing anything.
    Code:
    hwColorType():r(),g(),b() {}
    the : means that it is an initializer list. what it does is assign values to data members, usually private.

    so for example:
    Code:
    hwColorType(GLubyte rr, GLubyte gg, GLubyte bb)
    :r(rr),g(gg),b(bb) 
    {
    }
    woud be the same thing as:
    Code:
    hwColorType(GLubyte rr, GLubyte gg, GLubyte bb) {
        r = rr;
        g = gg;
        b = bb;
    }
    the syntax does look a bit weird at first. hope this helps some.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    32
    Thanks, but I meant the other one with the empty initializers?

    hwColorType():r(),g(),b(){}

    Ah well...I suppose I can live with it

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    oh, okay. I don't know. It looks like it doesn't do anything, which would make it useless except for another way to construct an object.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The empty initialisers are default ctor's they are normally called automatically thus
    hwColorType():r(),g(),b(){}
    is almost certainly identical to
    hwColorType(){}
    the long version donsn't hurt nothin though.
    If you dont specify any ctor's, a default one is created for you that does nothing (except sometimes some magic that it's best not to think about)
    If you do specify a ctor, then the default ctor is not created.
    Thus you do need this trivial default ctor It lets you do things like

    hwColorType colors[16];


    Init lists are not the same as assignments in the body of the ctor.
    hwColorType(GLubyte rr, GLubyte gg, GLubyte bb) : r(rr), g(gg), b(bb) {}
    calls GLubyte's copy ctor for r,g, and b, then returns.
    Code:
    hwColorType(GLubyte rr, GLubyte gg, GLubyte bb) {
        r = rr;
        g = gg;
        b = bb;
    }
    calls the default ctor's for r,g, and b, then calls the assignment operators for each. For GLubyte this makes not the slightest difference but, you will want to avoid doing this for more complex objects.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    32
    Thanks for clearing this up grib I suspected it to be like this I had never seen it before though.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i knew it was a default, just thought it should assign data to something. i know it doesn't need to be done, but...

    essentially, for constructors and the initilizer list v. assignment; i have usually seen it used to assign data members values. I have yet to learn more and experience more programming, so...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange number problem
    By kkjj in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 07:30 AM
  2. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  3. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM