Hello,
I am writing a C++ class, as follows:
class A
{
...
private:
int i;
B b;
}
where B is another class:
class B
{
...
private:
int j;
}
Now, my constructor for class A is:
A::A(int x) : i( x ) {
...
}
And the constructor for B is:
B::B(int y) : j( y ) {
...
}
So, my question is - how do I initialize b with variable i from within A's
constructor? I hope this makes sense, and thanks for any assistance.
Best,
Scott 5 1483
Scott wrote:
.... So, my question is - how do I initialize b with variable i from within A's constructor? I hope this makes sense, and thanks for any assistance.
If I understand what you mean, just do it exactly the same way you
initialized the other member:
A::A(int x) : i( x ), b( x ) {
...
}
On Sun, 05 Mar 2006 02:27:40 +0100, AnalogFile wrote:
Hi,
Thanks for the quick reply! Scott wrote: ... So, my question is - how do I initialize b with variable i from within A's constructor? I hope this makes sense, and thanks for any assistance.
If I understand what you mean, just do it exactly the same way you initialized the other member:
A::A(int x) : i( x ), b( x ) { ... }
Wouldn't this require b and x to be of the same type (i.e. int)? Making
it equivalent to writing b = x? But what I need is to construct an object
of type B, whose constructor needs an input of type int (i.e. B b( x ); ).
Another way of thinking about it - if I declare the following in class A:
class A
{
...
private:
B b;
}
And then in some method of A do this:
void
A::initB()
{
b = B( x );
}
Is that valid? If B only has a constructor of the form B::B( int y ),
then how does that ever get called from class A? Clearly I'm very
confused...
Maybe I should give a bit more detail...
In my main function, I define and declare b all at the same time, like so:
B b ( x );
But now I'm rewriting what occurs in main into it's own class (A). So b
becomes a private variable in class A, of type B (defined as above). b,
then, has already been declared, but I must still define it to call the
constructor, right?
I need a drink...
Best,
Scott
Scott wrote: On Sun, 05 Mar 2006 02:27:40 +0100, AnalogFile wrote:
Hi,
Thanks for the quick reply!
Scott wrote: ... So, my question is - how do I initialize b with variable i from within A's constructor? I hope this makes sense, and thanks for any assistance.
If I understand what you mean, just do it exactly the same way you initialized the other member:
A::A(int x) : i( x ), b( x ) { ... }
Wouldn't this require b and x to be of the same type (i.e. int)? Making it equivalent to writing b = x? But what I need is to construct an object of type B, whose constructor needs an input of type int (i.e. B b( x ); ).
the above construct actually looks for a matching constructor. for i(x) it
becomes an assignment statement i = x because i is a POD, but for b(x) where b
is a structured type it looks for a matching constructor, e.g. x is an int so
in this case it looks for a constructor B(int). If it finds this constructor
it will use it to create the embedded data member, otherwise you get an error.
David
On Sat, 04 Mar 2006 23:21:39 -0500, David Lindauer wrote: the above construct actually looks for a matching constructor. for i(x) it becomes an assignment statement i = x because i is a POD, but for b(x) where b is a structured type it looks for a matching constructor, e.g. x is an int so in this case it looks for a constructor B(int). If it finds this constructor it will use it to create the embedded data member, otherwise you get an error.
I see! You learn something new every day. Thanks for taking the time to
respond - I'll get to work implementing this.
Cheers,
Scott
David Lindauer wrote: Scott wrote: Scott wrote: If I understand what you mean, just do it exactly the same way you initialized the other member:
A::A(int x) : i( x ), b( x ) { ... }
Wouldn't this require b and x to be of the same type (i.e. int)? Making it equivalent to writing b = x? But what I need is to construct an object of type B, whose constructor needs an input of type int (i.e. B b( x ); ).
the above construct actually looks for a matching constructor. for i(x) it becomes an assignment statement i = x because i is a POD,
<nit, which doesn't affect the answer to the OP's question>
There's no assignment. i is initialised with the value of x. Equivalent
to
int i(x);
or
int i = x;
which initialise, but not
int i;
i = x;
which is assignment.
Gavin Deane This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Neil Zanella |
last post by:
Hello,
AFAIK the only way to initialize a reference variable defined inside a class
is to initialize it in an initializer list. However, when there are multiple
constructors, this means that the...
|
by: A J Le Couteur Bisson |
last post by:
Could someone please confirm that static class constructors are only called
at the first
use of a non-static constructor on the class, or am I doing something wrong?
If this is
indeed the case...
|
by: Pent |
last post by:
Hi All,
Why is this code valid? How can the static ctor be used? It doesn't act as
class ctor at all.
struct A
{
static A() { }
}
|
by: Dave |
last post by:
Hi everyone,
Is it possible, using an Attribute or by some other means, to notify the C#
Compiler to serialize all static field's that have initializers before code
in an explicit static...
|
by: John |
last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far.
I am now able to better define the question!
After adding more console printout lines to CSum, I tried all...
|
by: techiepundit |
last post by:
I have a class:
class ServerThreadManager(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# and a bunch of constructor statements
def run(self):
self.ReqHandlingLoop()
|
by: Hemanth |
last post by:
Hi,
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web, it appears
that there is no guarentee that this...
|
by: Richard |
last post by:
If class C inherits from class B, which inherits from class A, is the
order of the initializer list in the following constructor
insignificant (C only inherits from A because it inherits from B)?:...
|
by: DeveloperX |
last post by:
In an attempt to solve a problem further down I suggested the problem
might be caused by a missing constructor. Now, this led me to the
conclusion that I don't fully understand constructors, or...
|
by: =?Utf-8?B?QW5kcmVhcw==?= |
last post by:
Hi,
I would like to get some thoughts on Overloaded constructors vs. Object
initializations. Assuming that the class supports a default constructor, is
there any reason to include overloaded...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |