J
James Kanze
Still having question about this. I have try this:
class A {
public:
A(const A& a, const std::string& name) { cout << name << endl; }
};
file 1:
file 2:
And got no compilation error. In this case, what is the
behaviour ? is it well defined ?
It's defined behavior, but unspecified: both variables will
first be zero initialized, then one of the variable's
constructors will be called, then the other one. The first
constructor will see a zero initialized A const&.
You might try it, adding something to A so that you can see the
initialization, say:
class A
{
public:
A( A const& other, std::string const& name )
: myFirst( 1 )
, mySecond( other.myFirst )
{
std::cout << name << std::endl ;
}
int myFirst ;
int mySecond ;
} ;
Use this in the two files above, and try linking them in
different orders. After initialization, both objects will have
myFirst == 1, but one of them will have mySecond == 1, the other
== 0. Which one is not specified, but with most compilers,
you'll find that it will depend on the order the object files
are incorporated into the build. (In an unspecified way; you
can't reliably use this for controlling order of initialization,
even unportably.)