Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing parameter in member declaration

Reply
Thread Tools

passing parameter in member declaration

 
 
=?ISO-8859-15?Q?Sven_K=F6hler?=
Guest
Posts: n/a
 
      05-14-2007
Hi,

look at this example:

struct T1 {
bool v;

T1(bool p = false) {
this->v = p;
}
};

struct T2 {
T1 v1;
T1 v2(true);
};


The declaration of v1 is OK.
The declaration of v2 is not. I'd like to pass the parameter to the
construtor, but i can't imagine how.

How can i pass true to the constructor?
Is it possible at all?


Regards,
Sven
 
Reply With Quote
 
 
 
 
Ney =?ISO-8859-1?Q?Andr=E9?= de Mello Zunino
Guest
Posts: n/a
 
      05-14-2007
On Mon, 2007-05-14 at 03:16 +0200, Sven Köhler wrote:

> struct T1 {
> bool v;
>
> T1(bool p = false) {
> this->v = p;
> }
> };
>
> struct T2 {
> T1 v1;
> T1 v2(true);
> };
>
>
> The declaration of v1 is OK.
> The declaration of v2 is not. I'd like to pass the parameter to the
> construtor, but i can't imagine how.
>
> How can i pass true to the constructor?
> Is it possible at all?


If I understand your question correctly, all you have to do is write a
constructor for T2 and initialize its 'v2' member in the initialization
list:

T2::T2() :
v2(true) {
}

I hope that helps,

--
Ney André de Mello Zunino
http://zunino.eti.br/

 
Reply With Quote
 
 
 
 
Markus Schoder
Guest
Posts: n/a
 
      05-14-2007
On Mon, 14 May 2007 03:16:11 +0200, Sven Köhler wrote:

> Hi,
>
> look at this example:
>
> struct T1 {
> bool v;
>
> T1(bool p = false) {
> this->v = p;
> }
> };
>
> struct T2 {
> T1 v1;
> T1 v2(true);
> };
>
>
> The declaration of v1 is OK.
> The declaration of v2 is not. I'd like to pass the parameter to the
> construtor, but i can't imagine how.
>
> How can i pass true to the constructor? Is it possible at all?


Yes, you just need to do it in T2's constructor like so:

struct T2 {
T2() : v2(true) {}
T1 v1;
T1 v2;
};

--
Markus Schoder
 
Reply With Quote
 
fr3@K
Guest
Posts: n/a
 
      05-14-2007
On May 14, 9:16 am, Sven Khler <(E-Mail Removed)>
wrote:
> Hi,
>
> look at this example:
>
> struct T1 {
> bool v;
>
> T1(bool p = false) {
> this->v = p;
> }
>
> };
>
> struct T2 {
> T1 v1;
> T1 v2(true);
>
> };
>
> The declaration of v1 is OK.
> The declaration of v2 is not. I'd like to pass the parameter to the
> construtor, but i can't imagine how.
>
> How can i pass true to the constructor?
> Is it possible at all?
>
> Regards,
> Sven


Do that in T2's ctor:

struct T2 {
T1 v1;
T1 v2;

T2() : v2(true) {}
};

 
Reply With Quote
 
=?UTF-8?B?U3ZlbiBLw7ZobGVy?=
Guest
Posts: n/a
 
      05-14-2007
> Yes, you just need to do it in T2's constructor like so:
>
> struct T2 {
> T2() : v2(true) {}
> T1 v1;
> T1 v2;
> };


Exactly what i was looking for

Thanks!
 
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
Class declaration: TMember Member(); vs TMember Member; Skybuck Flying C++ 2 06-21-2011 02:36 PM
type declaration in declaration of a parameter or return type of a function Luca Forlizzi C Programming 4 11-14-2010 09:30 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Passing a member function as a parameter of a member function Azdo C++ 2 03-06-2007 11:21 AM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments