General C++ question

William J Deninger (deninger@uiuc.edu)
Sun, 3 May 1998 02:54:40 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_000C_01BD763E.D0F85EF0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

HELP!! Just when I thought I understood C++, I came across this (odd??) =
behavior.

Suppose you have the following class inheritance:

class TTest : public TBase =20
{
public:
TTest();
TTest(const TBase& source);

virtual ~TTest();

};

class TBase =20
{
public:
TBase();
TBase(const TBase& source);
virtual ~TBase();

};

TBase::TBase() { printf("TBase default constructor %p\n", this); }
TBase::TBase(const TBase &source) { printf("TBase copy constructor =
%p\n", this); }
TBase::~TBase() { printf("TBase destructor %p\n", this); }

TTest::TTest() : TBase() { printf("TText default constructor %p\n", =
this); }
TTest::TTest(const TBase& source) { printf("TText(TBase) copy =
constructor %p\n", this); }
TTest::~TTest() { printf("TText destructor %p\n", this);}

int main()
{
TTest test1;
TTest test2 =3D test1;
=20
printf("done\n");
=20
return;
}

// program output
TBase default constructor 0012FF70
TText default constructor 0012FF70
TBase copy constructor 0012FF6C
done
TText destructor 0012FF6C
TBase destructor 0012FF6C

What dont understand is how the various copy constructors are =
implemented. In the below example, the base class of test1 is called =
(why?? I didn't specify it using TTest() : TBase()). Also, for test2, =
the only constructor called is for the TBase. This doesn't seem to make =
sense to me. Is this correct, and if so is there any logic behind it??

William J Deninger
deninger@uiuc.edu

------=_NextPart_000_000C_01BD763E.D0F85EF0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">

HELP!!  Just when I thought I = understood=20 C++, I came across this (odd??) behavior.
 
Suppose you have the following class = inheritance:
 
class TTest : public TBase =20
{
public:
 TTest();
 TTest(const TBase&=20 source);
 
 virtual ~TTest();
 
};
 
class TBase =20
{
public:
 TBase();
 TBase(const TBase&=20 source);
 virtual ~TBase();
 
};
 
TBase::TBase() { printf("TBase = default=20 constructor %p\n", this); }
TBase::TBase(const TBase = &source) {=20 printf("TBase copy constructor %p\n", this); }
TBase::~TBase() { printf("TBase = destructor=20 %p\n", this); }

TTest::TTest() : TBase() {=20 printf("TText default constructor %p\n", this); }
TTest::TTest(const TBase& = source) { =20 printf("TText(TBase) copy constructor %p\n", this); = }
TTest::~TTest() {  = printf("TText=20 destructor %p\n", this);}
 
int main()
{
 TTest=20 test1;
 TTest test2 =3D test1;
 
 printf("done\n");
 
 return;
}
 
// program output
TBase default constructor = 0012FF70
TText=20 default constructor 0012FF70
TBase copy constructor = 0012FF6C
done
TText=20 destructor 0012FF6C
TBase destructor 0012FF6C
 
What dont understand is how the = various copy=20 constructors are implemented.  In the below example, the base class = of=20 test1 is called (why??  I didn't specify it using TTest() : = TBase()). =20 Also, for test2, the only constructor called is for the TBase.  = This=20 doesn't seem to make sense to me. Is this correct, and if so is there = any logic=20 behind it??
 
William J Deninger
deninger@uiuc.edu
 
 
 
------=_NextPart_000_000C_01BD763E.D0F85EF0--