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