473,386 Members | 1,720 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,386 software developers and data experts.

Constructor question...


I have an object ObjA (of class A) that contains another object ObjB (of
class B). When I construct object ObjA, I need to first get the value of x
before constructing an object B. I'm trying to avoid new() and delete().
How is it usually done?
class B
{
B(int y); // constructor
}

class A
{
A() // constructor
{
// first calculate x
int x = somefunc();
// then construct b
b(x); // wrong
}
B b;
}

Sep 11 '07 #1
5 1218
barcaroller wrote:
I have an object ObjA (of class A) that contains another object ObjB
(of class B). When I construct object ObjA, I need to first get the
value of x before constructing an object B. I'm trying to avoid
new() and delete(). How is it usually done?
class B
{
B(int y); // constructor
}

class A
{
A() // constructor
{
// first calculate x
int x = somefunc();
// then construct b
b(x); // wrong
}
B b;
}
;

Why can't you construct 'b' from the return value of 'somefunc'
in the 'A's initialiser list?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 11 '07 #2

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fc**********@news.datemas.de...
>
Why can't you construct 'b' from the return value of 'somefunc'
in the 'A's initialiser list?
Good point but if the function is more complex.
A() // constructor
{
// first calculate x, y, z
rc = somefunc(&x,&y,&z);
// then construct b
b(x,y,z); // wrong
}
Is A's initializer list the only place where b can be constructed?

Sep 12 '07 #3
"barcaroller" <ba*********@music.netwrote in message
news:fc**********@aioe.org...
>
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fc**********@news.datemas.de...
>>
Why can't you construct 'b' from the return value of 'somefunc'
in the 'A's initialiser list?

Good point but if the function is more complex.
A() // constructor
{
// first calculate x, y, z
rc = somefunc(&x,&y,&z);
// then construct b
b(x,y,z); // wrong
}
Is A's initializer list the only place where b can be constructed?
Without using new, yes. But even in your sample, you could call somefunc
outside the class and pass the values.

int main()
{
rc = somefunc(&x, &y, &z);

A Foo(x, y, z);
}

Or have B's constructor calculate x,y and z.

Or move the values that B needs into an initialization function in class B
that class A could then call.

A()
{
B b;
rc = somefunc(&x, &y, &z);

b.init(x,y,z);
}

There are many ways to do it.

Sep 12 '07 #4
barcaroller wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fc**********@news.datemas.de...
>>
Why can't you construct 'b' from the return value of 'somefunc'
in the 'A's initialiser list?

Good point but if the function is more complex.
A() // constructor
{
// first calculate x, y, z
rc = somefunc(&x,&y,&z);
// then construct b
b(x,y,z); // wrong
}
Is A's initializer list the only place where b can be constructed?
Constructed, yes. You can, of course, *assign* it in the body of
the constructor. You still need to provide a dummy argument and
initialise 'b' in the initialiser list.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '07 #5
On Sep 12, 2:19 am, "barcaroller" <barcarol...@music.netwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:fc**********@news.datemas.de...
Why can't you construct 'b' from the return value of 'somefunc'
in the 'A's initialiser list?
Good point but if the function is more complex.
A() // constructor
{
// first calculate x, y, z
rc = somefunc(&x,&y,&z);
// then construct b
b(x,y,z); // wrong
}
Is A's initializer list the only place where b can be
constructed?
Yes.

In addition to the other suggestions, I often will use something
like:

struct BInit { int x, int y, int z } ;

and a constructor which takes a BInit to B, then wrap somefunc
with a function which returns a BInit. If I don't have access
to B, this can be handled by trivial derivation. Something
like:

class A
{
// ...
struct BInit { int x, int y, int z } ;
static BInit bInit()
{
BInit result ;
somefunc( &result.x, &result.y, &result.z ) ;
return result ;
}
class MyB : public B
{
public:
MyB( BInit const& init )
: B( init.x, init.y, init.z )
{
}
} ;
MyB b ;

public:
A() : b( bInit() ) {}
} ;

Obviously, I'll only go to this effort if B isn't assignable, or
if the profiler tells me that default initialization plus
assignment is a bottleneck.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 12 '07 #6

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

Similar topics

10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
1
by: Bob Johnson | last post by:
Please note that this question is NOT about the merits of Microsoft certification (no need to get into that here). While taking a MS certification exam, I came across a question where it was...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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
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...

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.