pim42
May 5th, 2003, 02:32 PM
Ok so I think I'm having trouble with a class in my program so I created a small program to try to see what is going on. I am confused by the results. It seems that just when I think I understand C++ and OOP I find out I have been doing something wrong.
This is my BaseData class( in base.h):
class BaseData
{
public:
BaseData(){
static int ctor=0;
printf("ctor %d\n",ctor);
ctor++;
name = new char[30];
strcpy(name,"");
}
~BaseData(){
static int dtor=0;
printf("dtor %d\n",dtor);
dtor++;
delete[] name;
}
BaseData(const BaseData& p){
static int cctor=0;
printf("copy ctor %d\n",cctor);
cctor++;
name = new char[30];
*this = p;
}
BaseData& operator=(const BaseData& p){
static int assign=0;
printf("assign %d\n",assign);
assign++;
if(this != &p)
{
strcpy(name,p.name);
sensNum = p.sensNum;
}
return *this;
}
LPTSTR name;
BYTE sensNum;
};
This is my main:#include <windows.h>
#include <stdio.h>
#include "base.h"
int main()
{
BaseData b, t1;
BaseData* t2, t3;
strcpy(b.name,"Name");
b.sensNum = 42;
printf("b: %s %d\n",b.name,b.sensNum);
t1 = b;
printf("t1: %s %d\n",t1.name,t1.sensNum);
t2 = new BaseData(b);
printf("t2: %s %d\n",t2->name,t2->sensNum);
delete t2;
/*
t3 = new BaseData; //this statement won't compile
t3 = b;
printf("t3: %s %d\n",t3->name,t3->sensNum);
delete t3;
*/
return 0;
}
The output is:
ctor 0
ctor 1
ctor 2
b: Name 42
assign 0
t1: Name 42
copy ctor 0
assign 1
t2: Name 42
dtor 0
dtor 1
dtor 2
dtor 3
First off why is the dtor called 4 times? t3 is never created so why is it begin destroyed? Second, though I think this is a result of using printfs, why is ctor 2 printed before b: Name 42? Third and most important, the commented out portion doesn't work and I do not understand why. Can someone explain? Finally does this class leak memory and/or what is wrong with it if anything? (I just started having problems in my larger program and I think it has something to do with this class).
This is my BaseData class( in base.h):
class BaseData
{
public:
BaseData(){
static int ctor=0;
printf("ctor %d\n",ctor);
ctor++;
name = new char[30];
strcpy(name,"");
}
~BaseData(){
static int dtor=0;
printf("dtor %d\n",dtor);
dtor++;
delete[] name;
}
BaseData(const BaseData& p){
static int cctor=0;
printf("copy ctor %d\n",cctor);
cctor++;
name = new char[30];
*this = p;
}
BaseData& operator=(const BaseData& p){
static int assign=0;
printf("assign %d\n",assign);
assign++;
if(this != &p)
{
strcpy(name,p.name);
sensNum = p.sensNum;
}
return *this;
}
LPTSTR name;
BYTE sensNum;
};
This is my main:#include <windows.h>
#include <stdio.h>
#include "base.h"
int main()
{
BaseData b, t1;
BaseData* t2, t3;
strcpy(b.name,"Name");
b.sensNum = 42;
printf("b: %s %d\n",b.name,b.sensNum);
t1 = b;
printf("t1: %s %d\n",t1.name,t1.sensNum);
t2 = new BaseData(b);
printf("t2: %s %d\n",t2->name,t2->sensNum);
delete t2;
/*
t3 = new BaseData; //this statement won't compile
t3 = b;
printf("t3: %s %d\n",t3->name,t3->sensNum);
delete t3;
*/
return 0;
}
The output is:
ctor 0
ctor 1
ctor 2
b: Name 42
assign 0
t1: Name 42
copy ctor 0
assign 1
t2: Name 42
dtor 0
dtor 1
dtor 2
dtor 3
First off why is the dtor called 4 times? t3 is never created so why is it begin destroyed? Second, though I think this is a result of using printfs, why is ctor 2 printed before b: Name 42? Third and most important, the commented out portion doesn't work and I do not understand why. Can someone explain? Finally does this class leak memory and/or what is wrong with it if anything? (I just started having problems in my larger program and I think it has something to do with this class).