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 1494
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 initializer lists have to be cut and pasted
from one constructor to another. This does not seem to lend itself particularly
well to maintainablility. Calling a constructor from another in C++ is not legal
unlike in Java. Also, functions other...
|
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 then I would consider this a serious error in the language
implementation,
not to mention a pain in the backside :(
Also, is it to much to ask that the method Type.GetTypeFromCLSID be
documented
|
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 constructor?
Example:
public class MyClass
{
|
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 permutations
for constructors (none, default, two argument) and method call in body of
constructor (none and one).
Maybe this example is not representative, but for this example I found the
following:
1. Without any constructors, the program works fine...
| |
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 static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ? Is there any way to ensure the base class's static constructor is
invoked before the derived class instance is constructed ?...
|
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)?:
C::C() :
A(),
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 have made
lots of assumptions.
Things I (think) know, please correct as necessary:
Constructors are called on instantiation.
Static constructors are called the first time a class is referenced.
There can be many constructors and you can chain...
|
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 constructors when you are able to use
object initialization? I see a point, to provide the overloads, if you are
developing framework code that might support the need to run ontop of 2.x
but if you are writing a sealed application / know you are...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |