General C++ question

From: William J Deninger (deninger@uiuc.edu)
Date: Sun May 03 1998 - 09:54:40 MEST


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  
{
public:
 TTest();
 TTest(const TBase& source);

 virtual ~TTest();

};

class TBase  
{
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 = test1;
 
 printf("done\n");
 
 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



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:32 MET