passing parameter in member declaration

  • Thread starter =?ISO-8859-15?Q?Sven_K=F6hler?=
  • Start date
?

=?ISO-8859-15?Q?Sven_K=F6hler?=

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
 
N

Ney =?ISO-8859-1?Q?Andr=E9?= de Mello Zunino

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,
 
M

Markus Schoder

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;
};
 
F

fr3@K

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) {}
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top