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. 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
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
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.
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
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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;
|
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
|
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...
|
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
| |
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...
|
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?
|
by: sarathy |
last post by:
Hi,
What is integer promotion? How is it different from
arithmetic conversion?
Regards,
Sarathy
|
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...
|
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...
|
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: 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: 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...
|
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...
|
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...
|
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...
| |
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 ...
| |