Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13798

How member initialization list works?

From Stanley Rice <heconghui@gmail.com>
Newsgroups comp.lang.c++
Subject How member initialization list works?
Date 2012-02-05 19:12 -0800
Organization http://groups.google.com
Message-ID <445005.2165.1328497948225.JavaMail.geo-discussion-forums@prie27> (permalink)

Show all headers | View raw


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 ?

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

How member initialization list works? Stanley Rice <heconghui@gmail.com> - 2012-02-05 19:12 -0800
  Re: How member initialization list works? Goran <goran.pusic@gmail.com> - 2012-02-06 04:12 -0800
  Re: How member initialization list works? Juha Nieminen <nospam@thanks.invalid> - 2012-02-07 06:25 +0000

csiph-web