473,939 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator=


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 operator= function to copy
the base classes variables in the derived class (without just copying the
code?)

A much simplified example:

class Base
{
public:
Base();
~Base();

Base &operator=(cons t Base &B);

protected:
int i;
}

Base &Base::operator =(const Base &B)
{
if (this == &B)
return *this;
i = B.i;
return *this;
};
class Derived : public Base
{
Derived();
~Derived();

Derived &operator=(cons t Derived &D);

protected:
int n;
};

Derived &Derived::opera tor=(const Derived &B)
{
if (this == &B)
return *this;

// None of these work!
//(Base)(*this) = ::operator=B;
//(*this) =(const Base &)B;

n = B.n;
return *this;
};

And yet, in code not directly in these classes, this works fine:
Base b;
Derived d;
d = b;

Thanks!
Corey
Jun 27 '08 #1
17 1641
Hi,

make method "operator=" virtual, this should help. Non-virtual methods
cannot be overwritten, so you will always use operator= from class
Base.

Greetings from Bavaria,
Markus
Jun 27 '08 #2
On Jun 5, 10:09 am, "Corey Cooper" <Co...@EntecSys tems.netwrote:
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 operator= function to copy
the base classes variables in the derived class (without just copying the
code?)

A much simplified example:

class Base
{
public:
Base();
~Base();

Base &operator=(cons t Base &B);

protected:
int i;

}

Base &Base::operator =(const Base &B)
{
if (this == &B)
return *this;
i = B.i;
return *this;

};

class Derived : public Base
{
Derived();
~Derived();

Derived &operator=(cons t Derived &D);

protected:
int n;

};

Derived &Derived::opera tor=(const Derived &B)
{
if (this == &B)
return *this;

// None of these work!
//(Base)(*this) = ::operator=B;
//(*this) =(const Base &)B;

n = B.n;
return *this;

};

And yet, in code not directly in these classes, this works fine:
Base b;
Derived d;
d = b;

Thanks!
Corey
Derived &Derived::opera tor=(const Derived &B)
{
if (this == &B)
return *this;

// None of these work!
//(Base)(*this) = ::operator=B;
//(*this) =(const Base &)B;

n = B.n;
Base::operator =(B);

return *this;
};
Jun 27 '08 #3
On Jun 5, 2:09 am, "Corey Cooper" <Co...@EntecSys tems.netwrote:
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 operator= function to copy
the base classes variables in the derived class (without just copying the
code?)
Derived &Derived::opera tor=(const Derived &B)
{
if (this == &B)
return *this;
You want:

Base::operator = (B);
>
n = B.n;
return *this;

};

That's also the general syntax for calling the base implementation of
a function.

class Base {
virtual void f () { }
};

class Derived : public Base {
void f () {
Base::f();
// ...
}
};
Jason
Jun 27 '08 #4
On Jun 5, 2:32 am, cipher <markus.doersch m...@gmail.comw rote:
make method "operator=" virtual, this should help. Non-virtual methods
cannot be overwritten, so you will always use operator= from class
Base.
That will not help the main problem, though. Also, the derived
operator = takes a Derived&, not a Base&. Virtual or not, if you are
calling operator = on a Base, it will always use the Base one, e.g.:

void assign (Base &a, const Base &b) {
a = b;
}

Will always call Base::operator= only, no matter what derived classes
you pass it. Making operator = virtual in the Base will case
Derived::operat or= to be called only if there is a
Derived::operat or=(Base&), e.g.:
class Base {
public:
virtual Base & operator = (const Base &); // (1)
};

class Derived {
public:
Derived & operator = (const Derived &); // (2)
Derived & operator = (const Base &); // (3)
};

void assign (Base &a, const Base &b) {
a = b;
}

void f () {
Derived a, b;
a = b; // This calls (2).
assign(a, b); // This calls (3).
}
And assign(a, b) would call (1) if (1) wasn't virtual. But it will
never call (2).
Jason
Jun 27 '08 #5
You want:
>
Base::operator = (B);
>>
n = B.n;
return *this;

};


That's also the general syntax for calling the base implementation of
a function.
Ahh! I forgot that operator= is like any other function, and I kept wanting
to put something on the left of the equals sign!

Thanks All!
<ja************ @gmail.comwrote in message
news:f3******** *************** ***********@i76 g2000hsf.google groups.com...
On Jun 5, 2:09 am, "Corey Cooper" <Co...@EntecSys tems.netwrote:
>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 operator= function to copy
the base classes variables in the derived class (without just copying the
code?)
> Derived &Derived::opera tor=(const Derived &B)
{
if (this == &B)
return *this;

You want:

Base::operator = (B);
>>
n = B.n;
return *this;

};


That's also the general syntax for calling the base implementation of
a function.

class Base {
virtual void f () { }
};

class Derived : public Base {
void f () {
Base::f();
// ...
}
};
Jason

Jun 27 '08 #6
On Jun 5, 8:09 am, "Corey Cooper" <Co...@EntecSys tems.netwrote:
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 operator= function to copy the base classes
variables in the derived class (without just copying the
code?)
A much simplified example:
class Base
{
public:
Base();
~Base();
Base &operator=(cons t Base &B);
protected:
int i;
}
Base &Base::operator =(const Base &B)
{
if (this == &B)
What's this check for? It just wastes time, and is usually a
sign of other problems (but here, it's just unnecessary).
return *this;
i = B.i;
return *this;
};
class Derived : public Base
{
Derived();
~Derived();
Derived &operator=(cons t Derived &D);
protected:
int n;
};
Derived &Derived::opera tor=(const Derived &B)
{
if (this == &B)
(As above.)
return *this;
// None of these work!
//(Base)(*this) = ::operator=B;
//(*this) =(const Base &)B;
What's wrong with just:

Base::operator= ( B ) ;
n = B.n;
return *this;
};
And yet, in code not directly in these classes, this works fine:
Base b;
Derived d;
d = b;
It does? It shouldn't compile.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7
On Jun 5, 8:32 am, cipher <markus.doersch m...@gmail.comw rote:
make method "operator=" virtual,
Never make the operator= function virtual. It can't be made to
work correctly.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8

"James Kanze" <ja*********@gm ail.comwrote in message
news:d4******** *************** ***********@56g 2000hsm.googleg roups.com...
On Jun 5, 8:09 am, "Corey Cooper" <Co...@EntecSys tems.netwrote:
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 operator= function to copy the base classes
variables in the derived class (without just copying the
code?)
A much simplified example:
class Base
{
public:
Base();
~Base();
Base &operator=(cons t Base &B);
protected:
int i;
}
Base &Base::operator =(const Base &B)
{
if (this == &B)
What's this check for? It just wastes time, and is usually a
sign of other problems (but here, it's just unnecessary).
return *this;
i = B.i;
return *this;
};
class Derived : public Base
{
Derived();
~Derived();
Derived &operator=(cons t Derived &D);
protected:
int n;
};
Derived &Derived::opera tor=(const Derived &B)
{
if (this == &B)
(As above.)
return *this;
// None of these work!
//(Base)(*this) = ::operator=B;
//(*this) =(const Base &)B;
What's wrong with just:

Base::operator= ( B ) ;
n = B.n;
return *this;
};
>And yet, in code not directly in these classes, this works fine:
Base b;
Derived d;
d = b;
>It does? It shouldn't compile.
VC++ 6.0 it works.
c.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9
"Corey Cooper" <Co***@EntecSys tems.netwrote:
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=.
That sounds like a very bad idea.

void fn(Base& b, Derived& d)
{
b = d;
d = b;
}

AFAIK, in both of the above, Base::op=(const Base&) will be called, but
what if 'b' is a reference to a Derived object?

I don't think the base class should have an op= function. I think the
right thing to do is to make the Base::op= private and not implemented.
Jun 27 '08 #10

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

Similar topics

7
8048
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
1
3889
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include "BitString.h"
5
3819
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
0
1844
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should be disabled in a release build, and to do that efficiently, I suppose I need to overload the << operator. My current implementation of the stream in release mode is posted below. Everything works fine for POD type like bool and int, but the...
3
2968
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD FUNCTION!!
6
4441
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
18853
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<' token <snip> --------------------------------------------------- template <class T> class matriz;
5
2304
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
8
2499
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has something to do with the compiler calling the destructor twice. Could someone point out where I go wrong? P.S.: The error it gives is "Debug Assertion Failure ....." (at run time) P.P.S: Everything else works just fine (without the use of...
3
3300
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
0
10134
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11524
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11289
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9858
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6296
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4906
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.