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 2093
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**********@Ho me.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**********@Ho me.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**********@Ho me.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***********@n atinst.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 operator.
I am compelled to use the native C , instead of calling functions or using
macros for doing arithmetic operations.
|
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 compute it into the
result :)
I experimented a bit with strtok and the like, but I think I am not sure
how I should approach this problem. The problem being the priorities of
|
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 SubClass1 and SubClass2 which extend the
BaseClass and implement the virtual functions, so that I can go like this:
BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+newton/oskar;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |