Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13798 > unrolled thread
| Started by | Stanley Rice <heconghui@gmail.com> |
|---|---|
| First post | 2012-02-05 19:12 -0800 |
| Last post | 2012-02-07 06:25 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c++
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
| From | Stanley Rice <heconghui@gmail.com> |
|---|---|
| Date | 2012-02-05 19:12 -0800 |
| Subject | How member initialization list works? |
| Message-ID | <445005.2165.1328497948225.JavaMail.geo-discussion-forums@prie27> |
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 ?
[toc] | [next] | [standalone]
| From | Goran <goran.pusic@gmail.com> |
|---|---|
| Date | 2012-02-06 04:12 -0800 |
| Message-ID | <e8265f9c-d335-4f16-bf22-961682e69429@a15g2000yqf.googlegroups.com> |
| In reply to | #13798 |
On Feb 6, 4:12 am, Stanley Rice <hecong...@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2012-02-07 06:25 +0000 |
| Message-ID | <4f30c3d3$0$3095$7b1e8fa0@news.nbl.fi> |
| In reply to | #13798 |
Stanley Rice <heconghui@gmail.com> 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.)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web