Virtual arithmetic operators

C

Calum

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.
 
B

Bob Hairgrove

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?
 
V

Victor Bazarov

Calum said:
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
 
C

Calum

Bob said:
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;

Calum.
 
B

Bob Hairgrove

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?
 
C

Calum

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


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

V

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.
 
K

Kai-Uwe Bux

Calum said:
Victor said:
Yes. You already managed to declare the d-tor virtual, what's stopping
you from declaring operator- virtual?
[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
 
C

Calum

Kai-Uwe Bux said:
Calum said:
Victor said:
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: (e-mail address removed) if you are
interested...

Regards,
Calum.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top