Thread: don't understand "strange" constructor

  1. #1
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    don't understand "strange" constructor

    I found this constructor in a C++ book. Unfortunatly the authors didn't think it's worth any explanation or I'm too blind to find it

    Code:
    class iStack {
      public:
        iStack(int capacity): _stack(capacity), _top(0) {}
    
    
    [...snip...]
    
      private:
        int _top;
        vector<int> _stack;
    }
    I don't understand the blue part. What does it do? I would expect it to do nothing because there is nothing between { and } and there is no further implementation for the constructor, but _stack(capacity) looks like a possible constructor for the vector
    Does it initialize _stack and _top ? If so, why not beween the { and } ?
    [code]

    your code here....

    [/code]

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    That allows you to initialize a variable with out doing var = value; It is shorthand.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    so, this is the same?

    Code:
    iStack(int capacity) { _stack(capacity); _top(0) };
    Seems even shorter to me. Anyway, thanks
    [code]

    your code here....

    [/code]

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Yes, it's pretty much the same thing although using the method you have shown (called an initializer list), you can do things that you could not do any other way such as initialize a const data member.

    Code:
    class something
    {
        const int data;
    public:
        something(int val) : data(val) {}  // Ok, can initialize const member "data"
        something(int val)
        {
            data = val; // Error, can't initialize const member "data" like this
        }
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Code:
    constructor(): var(value) {} // is permitted
    constructor(): { var(value); }   is not permitted
    The second would make the compiler look for a function not a variable.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It's usually faster to use intializer list's then standard direct assignment.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    also, if your class is inherited, you can call the parent's constructor in the initializer list.

    there are many advantages to using an initializer list...
    I came up with a cool phrase to put down here, but i forgot it...

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by ...
    also, if your class is inherited, you can call the parent's constructor in the initializer list.

    there are many advantages to using an initializer list...
    As a matter of fact, it is the only place where you can call the parent's constructor. Also, anything that does not have a default constructor must be initialized in the initializer list.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM