Dear All,
I would like to know why the overriden operator = is not calles
properly by the function test in the following code. I really do not
understand it.
Thank you for any kind of help,
Przemyslaw
#include <iostream>
#include <cmath>
using namespace std;
class A {
public:
double a;
double b;
double c;
public:
A(const double& m_a, const double& m_b, const double& m_c)
{a=m_a; b=m_b; c=m_c;}
virtual ~A() {}
virtual A& operator = (const A&);
};
class B : public A {
public:
double d;
public:
B(const double& m_a, const double& m_b, const double& m_c, const
double& m_d) : A(m_a, m_b, m_c), d(m_d){}
~B() {}
B& operator = (const B&);
};
A& A::operator = (const A& m_A)
{
a = m_A.a;
b = m_A.b;
c = m_A.c;
std::cout << "base operator called " << std::endl;
return *this;
}
B& B::operator = (const B& m_B)
{
if(this!=&m_B)
(A&)(*this) = (A&)m_B;
a = m_B.a;
b = m_B.b;
c = m_B.c;
d = 12.4;
std::cout << "derived operator called " << std::endl;
return *this;
}
void test(A& a, const A& b) {a = b;}
int main()
{
A a(1, 2, 3);
B b(1, 2, 3, 4), c(0, 1, 2, 3);
test(b, c);
std::cout << a.a << " " << a.b << " " << a.c << std::endl;
std::cout << b.a << " " << b.b << " " << b.c << " " << b.d <<
std::endl;
std::cout << c.a << " " << c.b << " " << c.c << " " << c.d <<
std::endl;
return 0;
} 5 1113
Przemek wrote: Dear All,
I would like to know why the overriden operator = is not calles properly by the function test in the following code.
There is no overriden operator= in your example. The operator= in B has
another signature than the one in A, therefore it doesn't override the
one in A.
Przemek wrote: Dear All,
I would like to know why the overriden operator = is not calles properly by the function test in the following code. I really do not understand it.
Because for a virtual dispatch only the runtime type of the object
through which the call is made is relevant.
In void test(A& a, const A& b) {a = b;}
the compiler is searching for a function
operator=( constA& b );
and the only one it could find is in class A.
Since a is of type class B, which is derived from
class A, this one is taken.
The fact that b is in reality a class B object is
irrelevant when the correct function to call is
searched.
--
Karl Heinz Buchegger kb******@gascad.at
On 22 Jun 2004 03:26:56 -0700, pr**************@gazeta.pl (Przemek)
wrote: Dear All,
I would like to know why the overriden operator = is not calles properly by the function test in the following code. I really do not understand it.
[snip]
As others have pointed out, the arguments for operator= differ, so
they are overloaded, but not overriden functions (or did I get this
backwards again?).
One way of implementing such a polymorphic assignment operator would
be to make the base class operator= the only publicly accessible one.
It would not need to be virtual because it will not be overridden. Do
any assignment to base class members there and have an additional
protected pure virtual "assign()" function which the base class'
operator= calls. Override the protected "assign()" function, but hide
the operator= in the derived classes (by declaring it as private but
do not provide a body for it).
--
Bob Hairgrove No**********@Home.com
On Tue, 22 Jun 2004 13:38:50 +0200, Bob Hairgrove
<wouldnt_you_like@to_know.com> wrote:
[snip] protected pure virtual "assign()" function which the base class' operator= calls.
Of course, it is more complicated than that because you would need to
control the types you are assigning from / to.
IOW, if you have A<--B and A<--C, how do both B=C and C=B work
correctly? Or how could that be prevented?
--
Bob Hairgrove No**********@Home.com
Bob Hairgrove wrote: On Tue, 22 Jun 2004 13:38:50 +0200, Bob Hairgrove <wouldnt_you_like@to_know.com> wrote:
[snip]protected pure virtual "assign()" function which the base class' operator= calls.
Of course, it is more complicated than that because you would need to control the types you are assigning from / to.
IOW, if you have A<--B and A<--C, how do both B=C and C=B work correctly? Or how could that be prevented?
An object cannot change its type. The type will be the same from the
beginning to the end of its life time. Therefore, it's impossible in
C++ to use the assignment operator polymorphically in the way you (or
rather the OP) want. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Robert Sturzenegger |
last post by:
We had a strange problem in a large project with the use of a string class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see
below)....
|
by: Jef Driesen |
last post by:
Suppose I have an abstract base class that looks like this:
template <typename T>
class base {
public: // Typedefs
typedef double value_type;
typedef std::size_t size_type;
public:
//...
|
by: Tony Johansson |
last post by:
Hello!
Is this little nice main program a good solution?? or is it a bad solution.
I can't see any problem with it?
int main()
{
int *vektor = new int;
for (int i=0; i<3; i++)
vektor = i;
|
by: sshuangw |
last post by:
Hello:
I am encountering a very weird issue with MDI child, Overriden WndProc
function and hidden form.
Basically, the application has two forms, Form1(parent form),
Form2(Child form),...
|
by: NetronProject |
last post by:
My control inherites from ScrollableControl and overrides the BackColor
property. The DescriptionFromAttribute inherits from the
DescriptionAttribute to return localized information. The same for...
|
by: ThunderMusic |
last post by:
Hi,
I have a value that contains flags that I must get using a bitmask. I tryied
with the && operator, but the compiler outputs this error :
Operator '&&' cannot be applied to operands of type...
|
by: Mike |
last post by:
I am having a problem getting AND to work. I have a MySQL database field defined as varbinary(8)
with X00 in it. Then I have define statements with X01 in it.
define('CERTACCESS_MEDALS', x01);
...
|
by: noel.hunt |
last post by:
I have a base class, PadRcv, with virtual functions. User code
will derive from this class and possibly supply it's own functions
to override the base class virtual functions. How can I test that...
|
by: vivekian |
last post by:
Hi,
I have this following class
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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,...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
|
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...
| |