G
Guest
I'm having a problem with a derived class. When a copy is made of
the object it does indeed copy the additional member variables of
the object but it does not seem to be copying the members of the base class.
I have a class that is derived fom Cwnd, as follows:
#pragma once
#include "BitmapInformation.h"
// CCoolButton2p
class CCoolButton2p : public CWnd
{
DECLARE_DYNAMIC(CCoolButton2p)
public:
CCoolButton2p();
virtual ~CCoolButton2p();
protected:
DECLARE_MESSAGE_MAP()
private:
int m_nMyInt;
public:
CCoolButton2p operator=(CCoolButton2p other)
{
m_nMyInt = other.m_nMyInt;
return *this;
};
// Copy
CCoolButton2p(CCoolButton2p& other)
{
m_nMyInt = other.m_nMyInt;
};
};
It has just one member variable (in addition to whatever CWnd has), and I've
defined an operator= and a copy constructor.
I create an obect of this type and then add it to an array of these objects.
CCoolButton2p MyCoolButton;
myCoolButton.m_nMyInt = 27;
MyCoolButton.Create(NULL,
strLabel,
dwStyle,
rect,
pWndParent,
ID,
NULL);
// The MyCoolButton has memver values as expected:
// MyCoolButton.m_nMyInt is in fact 27
// MyCoolButton.m_hWnd is a valid window handle value
CArray<CCoolButton2p,CCoolButton2p> myArray;
myArray.Add(MyCoolButton);
CCoolButton2p MyCoolButtonFromTheArray;
MyCoolButtonFromTheArray = myArray.GetAt(0);
// The problem becomes obvious here:
// MyCoolButton.m_nMyInt is in fact 27, which is GOOD
// but
// MyCoolButton.m_hWnd is 0, which is bad.
// As is the copy constructor did not copy the udnerlying
// CWnd object.
At the point where I've done the Create() on the MyCoolButton I can
see that it has indeed created the window and the MyCoolButton.m_hWnd
does in fact have a valid window Handle. Also, the MyCoolButton.m_nMyInt
does indeed have the value that I set it to.
When I subsequently add the MyCoolButton to the myArray it (as expected)
invokes the copy constructor. But when I then examine the object that actually
got added I find that its m_nMyInt is correct, but the m_hWnd is zero! It's
as if
it copied the member variables of the derived class, but it did not also copy
the members of the base class.
What do I need to add/change in order to assure that it copies the members
of the underlying base class?
the object it does indeed copy the additional member variables of
the object but it does not seem to be copying the members of the base class.
I have a class that is derived fom Cwnd, as follows:
#pragma once
#include "BitmapInformation.h"
// CCoolButton2p
class CCoolButton2p : public CWnd
{
DECLARE_DYNAMIC(CCoolButton2p)
public:
CCoolButton2p();
virtual ~CCoolButton2p();
protected:
DECLARE_MESSAGE_MAP()
private:
int m_nMyInt;
public:
CCoolButton2p operator=(CCoolButton2p other)
{
m_nMyInt = other.m_nMyInt;
return *this;
};
// Copy
CCoolButton2p(CCoolButton2p& other)
{
m_nMyInt = other.m_nMyInt;
};
};
It has just one member variable (in addition to whatever CWnd has), and I've
defined an operator= and a copy constructor.
I create an obect of this type and then add it to an array of these objects.
CCoolButton2p MyCoolButton;
myCoolButton.m_nMyInt = 27;
MyCoolButton.Create(NULL,
strLabel,
dwStyle,
rect,
pWndParent,
ID,
NULL);
// The MyCoolButton has memver values as expected:
// MyCoolButton.m_nMyInt is in fact 27
// MyCoolButton.m_hWnd is a valid window handle value
CArray<CCoolButton2p,CCoolButton2p> myArray;
myArray.Add(MyCoolButton);
CCoolButton2p MyCoolButtonFromTheArray;
MyCoolButtonFromTheArray = myArray.GetAt(0);
// The problem becomes obvious here:
// MyCoolButton.m_nMyInt is in fact 27, which is GOOD
// but
// MyCoolButton.m_hWnd is 0, which is bad.
// As is the copy constructor did not copy the udnerlying
// CWnd object.
At the point where I've done the Create() on the MyCoolButton I can
see that it has indeed created the window and the MyCoolButton.m_hWnd
does in fact have a valid window Handle. Also, the MyCoolButton.m_nMyInt
does indeed have the value that I set it to.
When I subsequently add the MyCoolButton to the myArray it (as expected)
invokes the copy constructor. But when I then examine the object that actually
got added I find that its m_nMyInt is correct, but the m_hWnd is zero! It's
as if
it copied the member variables of the derived class, but it did not also copy
the members of the base class.
What do I need to add/change in order to assure that it copies the members
of the underlying base class?