473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

operator= for derived class

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?
Dec 26 '05 #1
3 1119

"Roger Garrett" <Ro**********@discussions.microsoft.com> skrev i
meddelandet news:15**********************************@microsof t.com...
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::operator=(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
Dec 27 '05 #2
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::operator=(other) results in a compiler error.
Dec 27 '05 #3
Roger Garrett wrote:
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
Dec 27 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
11
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
7
by: Jim Langston | last post by:
This is something someone was asking in irc. I really don't need to do this right now, but may have to in the future. The following code is in error (marked). #include <iostream> #include...
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
17
by: Corey Cooper | last post by:
I have a class for which I needed to create an operator= function. I then use that as a base class for another class, which also needs an operator=. What is the syntax to use in the derived class...
1
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.