473,503 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual arithmetic operators

Hi,

I have a base class called Number that has subclasses Integer and
Float. I want to be able to provide a virtual operator- in the base
class so that I can subtract one Integer from another and subtract one
Float from another (I'm not too bothered about subtracting an Integer
or Float from each other). Has anyone any idea on how to do this? My
code is below - the actual code that I want to add the operator- to is
a lot more complex but I thought I would keep things simple.

class Number
{
public:
virtual ~Number() {}
};

class Integer : public Number
{
public:
Integer(int value) : m_value(value) {}

private:
int m_value;
};

class Float : public Number
{
public:
Float(float value) : m_value(value) {}

private:
float m_value;
};

Thanks.

Mar 10 '06 #1
7 2077
On 10 Mar 2006 09:41:36 -0800, "Calum" <ca*************@gmail.com>
wrote:
Hi,

I have a base class called Number that has subclasses Integer and
Float. I want to be able to provide a virtual operator- in the base
class so that I can subtract one Integer from another and subtract one
Float from another (I'm not too bothered about subtracting an Integer
or Float from each other). Has anyone any idea on how to do this? My
code is below - the actual code that I want to add the operator- to is
a lot more complex but I thought I would keep things simple.

class Number
{
public:
virtual ~Number() {}
};

class Integer : public Number
{
public:
Integer(int value) : m_value(value) {}

private:
int m_value;
};

class Float : public Number
{
public:
Float(float value) : m_value(value) {}

private:
float m_value;
};

Thanks.


What type would the return value of such an operator have?

--
Bob Hairgrove
No**********@Home.com
Mar 10 '06 #2
Calum wrote:
I have a base class called Number that has subclasses Integer and
Float. I want to be able to provide a virtual operator- in the base
class so that I can subtract one Integer from another and subtract one
Float from another (I'm not too bothered about subtracting an Integer
or Float from each other). Has anyone any idea on how to do this?
Yes. You already managed to declare the d-tor virtual, what's stopping
you from declaring operator- virtual?
My
code is below - the actual code that I want to add the operator- to is
a lot more complex but I thought I would keep things simple.

class Number
{
public:
virtual ~Number() {}
};

class Integer : public Number
{
public:
Integer(int value) : m_value(value) {}

private:
int m_value;
};

class Float : public Number
{
public:
Float(float value) : m_value(value) {}

private:
float m_value;
};


Why don't you make an attempt to add operator- yourself? Otherwise it
seems too much like a homework assignment.

V
--
Please remove capital As from my address when replying by mail
Mar 10 '06 #3
Bob Hairgrove wrote:
On 10 Mar 2006 09:41:36 -0800, "Calum" <ca*************@gmail.com>
wrote:
Hi,

I have a base class called Number that has subclasses Integer and
Float. I want to be able to provide a virtual operator- in the base
class so that I can subtract one Integer from another and subtract one
Float from another (I'm not too bothered about subtracting an Integer
or Float from each other). Has anyone any idea on how to do this? My
code is below - the actual code that I want to add the operator- to is
a lot more complex but I thought I would keep things simple.

class Number
{
public:
virtual ~Number() {}
};

class Integer : public Number
{
public:
Integer(int value) : m_value(value) {}

private:
int m_value;
};

class Float : public Number
{
public:
Float(float value) : m_value(value) {}

private:
float m_value;
};

Thanks.


What type would the return value of such an operator have?

--
Bob Hairgrove
No**********@Home.com


The type should be Number&. I would like to be able to do something
like the following:

Integer a(7);
Integer b(5);
Number& result = a - b;

Calum.

Mar 10 '06 #4
On 10 Mar 2006 10:00:43 -0800, "Calum" <ca*************@gmail.com>
wrote:
What type would the return value of such an operator have?


The type should be Number&. I would like to be able to do something
like the following:

Integer a(7);
Integer b(5);
Number& result = a - b;


Bad idea to return a reference ... you need an object, and you cannot
return a Number object, can you?

--
Bob Hairgrove
No**********@Home.com
Mar 10 '06 #5
Victor Bazarov wrote:
Calum wrote:
I have a base class called Number that has subclasses Integer and
Float. I want to be able to provide a virtual operator- in the base
class so that I can subtract one Integer from another and subtract one
Float from another (I'm not too bothered about subtracting an Integer
or Float from each other). Has anyone any idea on how to do this?


Yes. You already managed to declare the d-tor virtual, what's stopping
you from declaring operator- virtual?
> My
code is below - the actual code that I want to add the operator- to is
a lot more complex but I thought I would keep things simple.

class Number
{
public:
virtual ~Number() {}
};

class Integer : public Number
{
public:
Integer(int value) : m_value(value) {}

private:
int m_value;
};

class Float : public Number
{
public:
Float(float value) : m_value(value) {}

private:
float m_value;
};


Why don't you make an attempt to add operator- yourself? Otherwise it
seems too much like a homework assignment.

V
--
Please remove capital As from my address when replying by mail


Like a homework assignment? I left school around 20 years ago and left
uny 9 years ago so what has homework got to do with it?

All I really want to know is can I call the - operator on two Number
pointers that are both pointing to Integer and how would I go about
this? Really, if you don't have anything useful to contribute then
don't bother posting. There again, looking at all of your other
postings, you come out with the same old whinings about referring other
people to other newsgroups blah blah blah......ad nauseum.

Mar 13 '06 #6
Calum wrote:
Victor Bazarov wrote:
Calum wrote:
> I have a base class called Number that has subclasses Integer and
> Float. I want to be able to provide a virtual operator- in the base
> class so that I can subtract one Integer from another and subtract one
> Float from another (I'm not too bothered about subtracting an Integer
> or Float from each other). Has anyone any idea on how to do this?
Yes. You already managed to declare the d-tor virtual, what's stopping
you from declaring operator- virtual?
> My
> code is below - the actual code that I want to add the operator- to is
> a lot more complex but I thought I would keep things simple.
>
> class Number
> {
> public:
> virtual ~Number() {}
> };
>
> class Integer : public Number
> {
> public:
> Integer(int value) : m_value(value) {}
>
> private:
> int m_value;
> };
>
> class Float : public Number
> {
> public:
> Float(float value) : m_value(value) {}
>
> private:
> float m_value;
> };

[snip] All I really want to know is can I call the - operator on two Number
pointers that are both pointing to Integer and how would I go about
this?


Since arithmetic operators like + take two arguments, the first problem is
that you want to choose a virtual function based on the dynamic types of
*two* objects, not just one. Google for double dispatch to get info on that
one.

However, why do you want to use pointers to numbers and virtual functions
anyway? I would prefer to just declare two types Integer and Float that
happen to implement similar interfaces and then, I would define conversion
functions from Integer to Float and rounding routines the other way round.
What do you gain by using runtime polymorphism?
Best

Kai-Uwe Bux
Mar 14 '06 #7
Kai-Uwe Bux wrote:
Calum wrote:
Victor Bazarov wrote:
Calum wrote:
> I have a base class called Number that has subclasses Integer and
> Float. I want to be able to provide a virtual operator- in the base
> class so that I can subtract one Integer from another and subtract one
> Float from another (I'm not too bothered about subtracting an Integer
> or Float from each other). Has anyone any idea on how to do this?

Yes. You already managed to declare the d-tor virtual, what's stopping
you from declaring operator- virtual?

> My
> code is below - the actual code that I want to add the operator- to is
> a lot more complex but I thought I would keep things simple.
>
> class Number
> {
> public:
> virtual ~Number() {}
> };
>
> class Integer : public Number
> {
> public:
> Integer(int value) : m_value(value) {}
>
> private:
> int m_value;
> };
>
> class Float : public Number
> {
> public:
> Float(float value) : m_value(value) {}
>
> private:
> float m_value;
> };

[snip]
All I really want to know is can I call the - operator on two Number
pointers that are both pointing to Integer and how would I go about
this?


Since arithmetic operators like + take two arguments, the first problem is
that you want to choose a virtual function based on the dynamic types of
*two* objects, not just one. Google for double dispatch to get info on that
one.

However, why do you want to use pointers to numbers and virtual functions
anyway? I would prefer to just declare two types Integer and Float that
happen to implement similar interfaces and then, I would define conversion
functions from Integer to Float and rounding routines the other way round.
What do you gain by using runtime polymorphism?
Best

Kai-Uwe Bux


Thanks Kai-Uwe,

Yes, I've already started looking into using double dispatch and it
looks promising.

The problem I have doesn't actually involve Integer and Float, but
something a bit more complex - I thought I would just post a simpler,
analogous problem... Anyway, I found an interesting approach by someone
who posted around ten years ago, though their solution had a few bits
of code missing! Message ID: 31***********@natinst.com if you are
interested...

Regards,
Calum.

Mar 14 '06 #8

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

Similar topics

12
1765
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?
5
2825
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
43
26443
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
12
2095
by: Raghu | last post by:
Hello all, I need your help on how to redefine teh function of arithmetic operators in C. for example : if there is an equation c = a/b; i want to execute my own algorithm for division...
335
11441
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
4
4686
by: Thomas Kreuzer | last post by:
Hello everyone, I have a question regarding how to interpret a string. Basically I want to write a little calculator, I will type something like "12+69*12-44/2" and then want my program to...
4
1523
by: moleskyca1 | last post by:
What operators cannot be virtual and why? I looked at FAQ and found nothing. I think there are operators that cannot be virtual, but I don't know why?
6
13799
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
3
1534
by: Jonas Huckestein | last post by:
Hello, I have the following problem: I have one BaseClass, which defines the virtual functions getValue(int index) and virtually overloads the operators *, /, -, +. Now I want two classes...
0
7086
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
7280
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,...
1
6991
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
7462
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...
0
5578
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,...
1
5014
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...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
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...
0
1512
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 ...

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.