operator= for derived class

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?
 
B

Bo Persson

Roger Garrett said:
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.

Because you don't call the copying or assignment code of the parent
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;

Add here:

CWnd::blush:perator=(other);

return *this;
};

// Copy
CCoolButton2p(CCoolButton2p& other)
{
m_nMyInt = other.m_nMyInt;
};

This calls the default constructor CWnd(), to call it's copy
constructor you should name it in the initialization list for your
class:

CCoolButton2p(CCoolButton2p& other) : CWnd(other),
m_nMyInt(other.m_nMyInt)
{ }
What do I need to add/change in order to assure that it copies the
members
of the underlying base class?

You need to call the corresponing code in the base class.

It's easy when you know it. :)


Bo Persson
 
G

Guest

Subsequent testing, as well as advice from others, indicates that the CWnd
does not allow calling its copy constructor or assignmentoperator (operator=)
because they are both declared as provate to the CWnd class. In other words,
you can't copy or assign a CWnd object.

Doing the suggested CWnd::blush:perator=(other) results in a compiler error.
 
C

Carl Daniel [VC++ MVP]

Roger said:
Subsequent testing, as well as advice from others, indicates that the
CWnd does not allow calling its copy constructor or
assignmentoperator (operator=) because they are both declared as
provate to the CWnd class. In other words, you can't copy or assign a
CWnd object.

A CWnd is just a thin wrapper over an HWND and windows handles can't be
meaningfully duplicated in MFC. One window, one handle. MFC requires a 1:1
mapping between an HWND and the correspoding CWnd because the address of the
CWnd is stored in the user data of the window and there's only room for one.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top