A
APT News Address
Sorry if this is a dumb question.
I'm having a problem with a class I designed. For some reason the
destructor is being called before I am done with it and I can't see a
reason for it. The constructor appears to work fine when it is called
for both objects I create but for some reason the destructor is called
immediately after construction in the second of these objects. Because
of this the destructor crashes the program the second time it is
called when the object goes out of scope. Can anyone help me to see
what I'm doing wrong.
Thanks,
-Adrian
The code is as follows:
MainFile.cpp
============
#include "stdafx.h"
#include "Parameter.h"
int _tmain(int argc, TCHAR* argv[])
{
TCHAR* pp = 0;
CParameter P1("Hello");
CParameter P2(pp);
// Between these two lines the destructor for P2 executes
// for some reason, I can't see a reason for this.
return 0;
}
Parameter.h
===========
#pragma once
class CParameter
{
public:
CParameter(void);
CParameter(const TCHAR Name[]);
CParameter( const CParameter &rhs );
~CParameter(void);
const TCHAR* const Parameter(void);
const bool Explicit(void);
inline CParameter& operator=( const CParameter &rhs );
private:
bool _Explicit;
TCHAR* _Parameter;
};
Parameter.cpp
=============
#include "StdAfx.h"
#include ".\parameter.h"
CParameter::CParameter(void)
{
_Parameter = 0;
_Explicit = false;
}
CParameter::CParameter(const TCHAR Name[])
{
if( Name )
{
_Parameter = new TCHAR[ strlen(Name) + 1 ];
strcpy( _Parameter, Name );
_Explicit = true;
}
else
{
CParameter();
}
}
CParameter::CParameter( const CParameter &rhs )
{
_Parameter = 0;
*this = rhs;
}
CParameter::~CParameter(void)
{
delete [] _Parameter;
}
const TCHAR* const CParameter:arameter(void)
{
if( _Parameter )
return _Parameter;
else
return 0;
}
const bool CParameter::Explicit(void)
{
return _Explicit;
}
inline CParameter& CParameter:perator=( const CParameter &rhs )
{
if( this != &rhs )
{
delete [] _Parameter;
if( rhs._Parameter )
_Parameter = new TCHAR[ strlen(rhs._Parameter) + 1 ];
else
_Parameter = 0;
strcpy( _Parameter, rhs._Parameter );
_Explicit = rhs._Explicit;
}
return *this;
}
I'm having a problem with a class I designed. For some reason the
destructor is being called before I am done with it and I can't see a
reason for it. The constructor appears to work fine when it is called
for both objects I create but for some reason the destructor is called
immediately after construction in the second of these objects. Because
of this the destructor crashes the program the second time it is
called when the object goes out of scope. Can anyone help me to see
what I'm doing wrong.
Thanks,
-Adrian
The code is as follows:
MainFile.cpp
============
#include "stdafx.h"
#include "Parameter.h"
int _tmain(int argc, TCHAR* argv[])
{
TCHAR* pp = 0;
CParameter P1("Hello");
CParameter P2(pp);
// Between these two lines the destructor for P2 executes
// for some reason, I can't see a reason for this.
return 0;
}
Parameter.h
===========
#pragma once
class CParameter
{
public:
CParameter(void);
CParameter(const TCHAR Name[]);
CParameter( const CParameter &rhs );
~CParameter(void);
const TCHAR* const Parameter(void);
const bool Explicit(void);
inline CParameter& operator=( const CParameter &rhs );
private:
bool _Explicit;
TCHAR* _Parameter;
};
Parameter.cpp
=============
#include "StdAfx.h"
#include ".\parameter.h"
CParameter::CParameter(void)
{
_Parameter = 0;
_Explicit = false;
}
CParameter::CParameter(const TCHAR Name[])
{
if( Name )
{
_Parameter = new TCHAR[ strlen(Name) + 1 ];
strcpy( _Parameter, Name );
_Explicit = true;
}
else
{
CParameter();
}
}
CParameter::CParameter( const CParameter &rhs )
{
_Parameter = 0;
*this = rhs;
}
CParameter::~CParameter(void)
{
delete [] _Parameter;
}
const TCHAR* const CParameter:arameter(void)
{
if( _Parameter )
return _Parameter;
else
return 0;
}
const bool CParameter::Explicit(void)
{
return _Explicit;
}
inline CParameter& CParameter:perator=( const CParameter &rhs )
{
if( this != &rhs )
{
delete [] _Parameter;
if( rhs._Parameter )
_Parameter = new TCHAR[ strlen(rhs._Parameter) + 1 ];
else
_Parameter = 0;
strcpy( _Parameter, rhs._Parameter );
_Explicit = rhs._Explicit;
}
return *this;
}