Virtual arithmetic operators

Discussion in 'C++' started by Calum, Mar 10, 2006.

  1. Calum

    Calum Guest

    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.
     
    Calum, Mar 10, 2006
    #1
    1. Advertising

  2. On 10 Mar 2006 09:41:36 -0800, "Calum" <>
    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
     
    Bob Hairgrove, Mar 10, 2006
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Mar 10, 2006
    #3
  4. Calum

    Calum Guest

    Bob Hairgrove wrote:
    > On 10 Mar 2006 09:41:36 -0800, "Calum" <>
    > 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
    >


    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.
     
    Calum, Mar 10, 2006
    #4
  5. On 10 Mar 2006 10:00:43 -0800, "Calum" <>
    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
     
    Bob Hairgrove, Mar 10, 2006
    #5
  6. Calum

    Calum Guest

    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, Mar 13, 2006
    #6
  7. Calum

    Kai-Uwe Bux Guest

    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, Mar 14, 2006
    #7
  8. Calum

    Calum Guest

    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: if you are
    interested...

    Regards,
    Calum.
     
    Calum, Mar 14, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Christian_Brechb=FChler?=

    Enhancing valarray with "normal" arithmetic operators

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003, in forum: C++
    Replies:
    6
    Views:
    961
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Sep 14, 2003
  2. Lionel B
    Replies:
    5
    Views:
    458
    Gianni Mariani
    Mar 3, 2005
  3. Trent Buck

    variadic arithmetic, boolean operators

    Trent Buck, Jan 1, 2005, in forum: C Programming
    Replies:
    3
    Views:
    410
    Mysidia
    Jan 2, 2005
  4. joshc
    Replies:
    5
    Views:
    559
    Keith Thompson
    Mar 31, 2005
  5. Raghu

    How to redefine arithmetic operators.

    Raghu, Nov 29, 2005, in forum: C Programming
    Replies:
    12
    Views:
    548
    Eric Sosman
    Nov 30, 2005
Loading...

Share This Page