473,320 Members | 1,940 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,320 software developers and data experts.

No polymiorphism for virtual operator= ?

Hi All,
I tried the following code, but it did not work as I think:

---
using namespace std;
namespace
{
class Fraction
{
public:
Fraction(int num1, int num2): m_num1(num1), m_num2(num2){};
virtual Fraction& operator=(const Fraction& f);
private:
int m_num1;
int m_num2;
};
Fraction& Fraction::operator =(const Fraction& f)
{
cout << "Fraction::operator=" << "\n";
return *this;
}

class Fraction2: public Fraction
{
public:
Fraction2(int num1, int num2, int num3): Fraction(num1, num2),
m_num3(num3){}
virtual Fraction2& operator=(const Fraction2& f);
private:
int m_num3;
};

Fraction2& Fraction2::operator=(const Fraction2& f)
{
cout << "Fraction2::operator=" << "\n";
return *this;
}
}

void Sample(Fraction& f1, Fraction& f2)
{
f1 = f2;
}

int _tmain(int argc, _TCHAR* argv[])
{
Fraction2 f1(1, 2, 3);
Fraction2 f2(3, 4, 5);

Sample(f1, f2);

system("pause");
return 0;
}

output: Fraction::operator=
---

Is it because there's no polymiorphism for operator= or what?

Thanks in advance.

Jul 23 '07 #1
8 2517
On Jul 23, 5:11 pm, sun1991 <sun1...@gmail.comwrote:
Hi All,
I tried the following code, but it did not work as I think:

---
using namespace std;
namespace
{
class Fraction
{
public:
Fraction(int num1, int num2): m_num1(num1), m_num2(num2){};
virtual Fraction& operator=(const Fraction& f);
private:
int m_num1;
int m_num2;
};
Fraction& Fraction::operator =(const Fraction& f)
{
cout << "Fraction::operator=" << "\n";
return *this;
}

class Fraction2: public Fraction
{
public:
Fraction2(int num1, int num2, int num3): Fraction(num1, num2),
m_num3(num3){}
virtual Fraction2& operator=(const Fraction2& f);
private:
int m_num3;
};

Fraction2& Fraction2::operator=(const Fraction2& f)
{
cout << "Fraction2::operator=" << "\n";
return *this;
}

}

void Sample(Fraction& f1, Fraction& f2)
{
f1 = f2;
This is same as saying f1.operator=(f2);

Since C++ is statically typed, type of f1 is determined at compile
time, which turns out to be 'Fraction'. Thus, there is no way
"operator=" from Fraction2 will get called.

>
}

int _tmain(int argc, _TCHAR* argv[])
non-standard.
{
Fraction2 f1(1, 2, 3);
Fraction2 f2(3, 4, 5);

Sample(f1, f2);

system("pause");
return 0;

}

output: Fraction::operator=
---

Is it because there's no polymiorphism for operator= or what?

-N

Jul 23 '07 #2
On Jul 23, 2:11 pm, sun1991 <sun1...@gmail.comwrote:
Hi All,
I tried the following code, but it did not work as I think:

---
.... boring and irrelevent stuff deleted ...
using namespace std;
class Fraction
{
public:
virtual Fraction& operator=(const Fraction& f);
};
class Fraction2: public Fraction
{
public:
virtual Fraction2& operator=(const Fraction2& f);
};
}
Is it because there's no polymiorphism for operator= or what?
No. It is because, you have acually two operator='s in F2. The first
inherited from Fraction and taking Fraction& as rhs and the second
new one with F2 as rhs. To actually override the Fraction's
opertator=,
you need the operator= to have exactly the same arguments, ie.

Fraction2& Fraction2::operator=(const Fraction&);

I think (but am not sure) it's possible to return Fraction2 here.

You will probably have to use dynamic_cast to downcast the
rhs from Fraction to Fraction2

Regards
Jiri Palecek

Jul 23 '07 #3
On Jul 23, 2:23 pm, "Alf P. Steinbach" <al...@start.nowrote:
* sun1991:
int _tmain(int argc, _TCHAR* argv[])

This is not a standard "main" function.

Therefore, your program can produce /any/ output, if it compiles with
your compiler.
That's nonsense, read section 4.6 in the standard.

If you have nothing to say, please refrain from posting.

Jiri Palecek

Jul 23 '07 #4
On 2007-07-23 16:29, jp******@web.de wrote:
On Jul 23, 2:23 pm, "Alf P. Steinbach" <al...@start.nowrote:
>* sun1991:
int _tmain(int argc, _TCHAR* argv[])

This is not a standard "main" function.

Therefore, your program can produce /any/ output, if it compiles with
your compiler.

That's nonsense, read section 4.6 in the standard.

If you have nothing to say, please refrain from posting.
It's section 3.6.1 in my copy, and it says quite clearly that the
function should be named "main" and nothing else (such as "_tmain").

--
Erik Wikström
Jul 23 '07 #5
>
to make it work, you have to do this:

class Fraction2: public Fraction
{
public:
Fraction2(int num1, int num2, int num3):
Fraction(num1, num2),
m_num3(num3){}
virtual Fraction& operator=(const Fraction& f);
private:
int m_num3;
};

but this really doesn't make too much sense.

Yeah, I got your point. Thanks. I used different parameter in
operator=, so it becomes two different function. Can I say that don't
expect virtual operator= to be polymiorphism, because it is not the
way you want? (Sounds like a C++ philosophy question)
Jul 24 '07 #6
hi sory!!!! am new that work

Jul 24 '07 #7
>
Yeah, I got your point. Thanks. I used different parameter in
operator=, so it becomes two different function. Can I say that don't
expect virtual operator= to be polymiorphism, because it is not the
way you want? (Sounds like a C++ philosophy question)

If you keep the signature the same (as in the code shown above) in the
derived class, then you have a polymorphic assignment operator. It's an
assignment operator but it's not the copy assignment operator; you can
have as many assignment operators as you want in addition to the copy
assignment operator. When the copy assignment operator is not declared,
and is needed, the compiler generates a copy assignment operator.
When I say philosophy, I mean people usually don't do that, because it
is often a bad design (try to make operator= polymiorphism), isn't it?

Jul 24 '07 #8
On Jul 24, 11:30 am, sun1991 <sun1...@gmail.comwrote:
Yeah, I got your point. Thanks. I used different parameter in
operator=, so it becomes two different function. Can I say that don't
expect virtual operator= to be polymiorphism, because it is not the
way you want? (Sounds like a C++ philosophy question)
If you keep the signature the same (as in the code shown above) in the
derived class, then you have a polymorphic assignment operator. It's an
assignment operator but it's not the copy assignment operator; you can
have as many assignment operators as you want in addition to the copy
assignment operator. When the copy assignment operator is not declared,
and is needed, the compiler generates a copy assignment operator.
When I say philosophy, I mean people usually don't do that,
because it is often a bad design (try to make operator=
polymiorphism), isn't it?
In general, polymophism and value semantics (copy and
assignment) don't go well together. Consider the following:

Base* p1 = new Derived1 ;
Base* p2 = new Derived2 ;
*p1 = *p2 ;

logically, you would expect *p1 and *p2 to be equal after that
(for some suitable definition of equal). But in fact, they will
still have different types; there's no way to dynamically change
the type of an object once it has been created. For this
reason, *most* polymorphic hierarchies will ban assignment (and
make copy private, so that it can only be used in a clone()
function, which creates a new instance).

If you really need value semantics and polymorphism, look up the
letter-envelop idiom. Most of the time, however, I think you'll
find that just using a pointer and/or references is
adequate---this is one case where boost::shared_ptr is often
appropriate.

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

Jul 24 '07 #9

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

Similar topics

3
by: CoolPint | last post by:
I read that the return type has to be exactly same for a virtual function to be overriden. While testing something, I discovered something I cannot understand. I cannot understand why the code...
12
by: c++novice | last post by:
1--Can operators be virtual? 2--What is the difference between an operator= returning a refernce Vs a value?
17
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how...
15
by: Heiner | last post by:
#include <stdio.h> class A { public: virtual A & operator= (const A &); virtual void test(const A &); }; class B : public A
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
2
by: sun1991 | last post by:
I recently come to this thought, and did a test: class B1 { public: virtual void Func() { std::cout << "Here is B1" << "\n"; } };
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.