473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

passing parameter in member declaration

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
May 14 '07 #1
4 1342
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/

May 14 '07 #2
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
May 14 '07 #3
fr3
On May 14, 9:16 am, Sven Köhler <remove-for-no-spam-skoeh...@upb.de>
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) {}
};

May 14 '07 #4
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!
May 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
13
by: Amadeus W. M. | last post by:
I have a member static const int x defined in class Foo, and I'm passing it by reference, and by value elsewhere (see the code below). Passing it by value works, but by reference it doesn't: it...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
4
by: Ondrej Spanel | last post by:
The code below does not compile with .NET 2003, I get folowing error: w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' : illegal type for non-type template parameter 'x' ...
5
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.