Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How member initialization list works?

Reply
Thread Tools

How member initialization list works?

 
 
Stanley Rice
Guest
Posts: n/a
 
      02-06-2012
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 ?
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      02-06-2012
On Feb 6, 4:12*am, Stanley Rice <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      02-07-2012
Stanley Rice <(E-Mail Removed)> 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.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
error C2614: 'CYYYRegister' : illegal member initialization:'CRequest' is not a base or member Angus C++ 1 03-06-2008 11:38 AM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM



Advertisments