Re: ctors defined twice in .h and .C files
wenmang@yahoo.com wrote:
Here is the code which causes core dump when is built differently, 1
for regular dev built, the other for packaging build.
class my_class{
public:
my_class(){};
~my_class(){};
Drop the semicolons after function bodies, it will be easier to read
and find errors like that.
private:
string myData1;
string myData2;
};
x.C file:
my_class::my_class() : myData1(), myData2()
{
}
my_class::~my_class()
{
}
Obviously, ctor/dtor are defined twice in .h and .C files. Here is my
question:
1. are myData1 and myData2 are initialized or not when ctor/dtor
defined in .h are called?
They are default-initialised regardless of the fact that they are not
mentioned in the [missing] initialiser list.
However, since the program violates the ODR, its behaviour is undefined.
2. What behaviour of compiler if ctor/dtor are defined twice?
The behaviour is undefined. It's a violation of the ODR.
3. debug trace indicates that one of two strings are not initialized,
Behaviour is undefined, anything can happen.
here is debug output:
debug> p myData1
{
_String_base::_M_start=NULL
_String_base::_M_finish=NULL
_String_base::_M_end_of_storage=NULL
static basic_string::npos=4294967295
Looks like a default-initialised string to me.
}
Can I safely say that myData1 memory is not allocated at this point,
if string assignment happens, it will lead core-dump, right?
No. Assignment calls a function. Step into it and see how it does
its thing.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask