How member initialization list works?

Discussion in 'C++' started by Stanley Rice, Feb 6, 2012.

  1. Stanley Rice

    Stanley Rice Guest

    Hello all

    In the following class definition:

    class iStack {
    public:
    iStack( int capacity )
    : _stack( capacity ), _top( 0 ) {}
    // ...
    private:
    vector< int > _stack;
    int _top;
    };

    The date member _stack is initialized in the form _stack(capacity) in the
    member initialized list. My question, how it could be? the type of _stact
    is vector<int>, but the type of capacity is int.

    I wander if the conversion function is called? Then I wrote the following
    code:
    vector<int> va = 2;
    but the compiler complains.

    However, If the define another class, say,

    class A
    {
    public:
    A(int a) : ia(a) {}

    private:
    int ia;
    };

    Then the code
    A ca = 2;
    pass.

    What's the difference ?
     
    Stanley Rice, Feb 6, 2012
    #1
    1. Advertising

  2. Stanley Rice

    Goran Guest

    On Feb 6, 4:12 am, Stanley Rice <> wrote:
    > Hello all
    >
    > In the following class definition:
    >
    > class iStack {
    >     public:
    >         iStack( int capacity )
    >             : _stack( capacity ), _top( 0 ) {}
    >         // ...
    >     private:
    >         vector< int > _stack;
    >         int _top;
    >
    > };
    >
    > The date member _stack is initialized in the form _stack(capacity) in the
    > member initialized list. My question, how it could be? the type of _stact
    > is vector<int>, but the type of capacity is int.
    >
    > I wander if the conversion function is called? Then I wrote the following
    > code:
    >     vector<int> va = 2;
    > but the compiler complains.
    >
    > However, If the define another class, say,
    >
    > class A
    > {
    > public:
    >     A(int a) : ia(a) {}
    >
    > private:
    >     int ia;
    >
    > };
    >
    > Then the code
    >     A ca = 2;
    > pass.
    >
    > What's the difference ?


    1. keyword "explicit"
    2. (unrelated, I think) vector can be created off a size_t (unsigned).
    You tried signed.
     
    Goran, Feb 6, 2012
    #2
    1. Advertising

  3. Stanley Rice <> wrote:
    > In the following class definition:
    >
    > class iStack {
    > public:
    > iStack( int capacity )
    > : _stack( capacity ), _top( 0 ) {}
    > // ...
    > private:
    > vector< int > _stack;
    > int _top;
    > };
    >
    > The date member _stack is initialized in the form _stack(capacity) in the
    > member initialized list. My question, how it could be? the type of _stact
    > is vector<int>, but the type of capacity is int.


    You are calling the std::vector constructor that takes takes a size_type
    and a value of the member type (in this case int). The latter has a default
    value, which is why you don't have to specify it. The former initializes the
    vector with the specified amount of elements, which is what the code above
    is doing.

    (Ok, technically speaking that constructor takes three parameters, the
    third one being an allocator. But that one also has a default value so
    you don't have to specify it.)
     
    Juha Nieminen, Feb 7, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    889
    Peter_Julian
    Oct 10, 2005
  2. Replies:
    6
    Views:
    481
    Ron Natalie
    Dec 11, 2005
  3. toton
    Replies:
    5
    Views:
    949
    Victor Bazarov
    Sep 28, 2006
  4. Angus
    Replies:
    1
    Views:
    2,704
  5. aaragon
    Replies:
    2
    Views:
    637
    James Kanze
    Nov 2, 2008
Loading...

Share This Page