Constructor question...

B

barcaroller

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

Victor Bazarov

barcaroller said:
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
 
B

barcaroller

Victor Bazarov said:
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?
 
J

Jim Langston

barcaroller said:
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.
 
V

Victor Bazarov

barcaroller said:
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
 
J

James Kanze

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.
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top