Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Arithmetic Operator Overload in Pure Abstract Class

Arithmetic Operator Overload in Pure Abstract Class

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancehelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CJ1
    wrote on last edited by
    #1

    This is a tough one, I want to overload the arithmetic operators in a pur virtual abstract class.:confused: The pur abstract class defines the basic 'Interface' from there I have a base parent class that will be the default implementation and several parallel child classes. e.g.

    virtual IClass operator+(IClass& RHS) = 0;

    Then I can implement it in BaseClass. Problem: Can not instantiate abstract class. changing it to

    virtual IClass& operator+(IClass& RHS) = 0;

    means that the statement is valid BUT it can't work (or can it?) this returns a reference to IClass which isn't what we want in addition, we want to return a value not a reference. So how can I do this whithou getting in to memory leaks and such. For addition we want to be able to do result = a + b + c + d, etc. This is pure old (not C++11) C++ I'm working in today.

    CJ

    J S 2 Replies Last reply
    0
    • C CJ1

      This is a tough one, I want to overload the arithmetic operators in a pur virtual abstract class.:confused: The pur abstract class defines the basic 'Interface' from there I have a base parent class that will be the default implementation and several parallel child classes. e.g.

      virtual IClass operator+(IClass& RHS) = 0;

      Then I can implement it in BaseClass. Problem: Can not instantiate abstract class. changing it to

      virtual IClass& operator+(IClass& RHS) = 0;

      means that the statement is valid BUT it can't work (or can it?) this returns a reference to IClass which isn't what we want in addition, we want to return a value not a reference. So how can I do this whithou getting in to memory leaks and such. For addition we want to be able to do result = a + b + c + d, etc. This is pure old (not C++11) C++ I'm working in today.

      CJ

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      So you have the following class Child1 : IClass class Child2 : IClass What do you think the following is going to do? Child1 op1 = ... Child2 op2 = ... Child1 result1 = op1 + op2; Child1 result2 = op2 + op1;

      1 Reply Last reply
      0
      • C CJ1

        This is a tough one, I want to overload the arithmetic operators in a pur virtual abstract class.:confused: The pur abstract class defines the basic 'Interface' from there I have a base parent class that will be the default implementation and several parallel child classes. e.g.

        virtual IClass operator+(IClass& RHS) = 0;

        Then I can implement it in BaseClass. Problem: Can not instantiate abstract class. changing it to

        virtual IClass& operator+(IClass& RHS) = 0;

        means that the statement is valid BUT it can't work (or can it?) this returns a reference to IClass which isn't what we want in addition, we want to return a value not a reference. So how can I do this whithou getting in to memory leaks and such. For addition we want to be able to do result = a + b + c + d, etc. This is pure old (not C++11) C++ I'm working in today.

        CJ

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        One way to do it is by adding (pure) virtual accessors that read the elements that you need for the addition, and (pure) virtual factory methods that you can use to create an object of the correct type. The following might work:

        class vecbase {
        public:
        virtual int size() const = 0;
        virtual int& operator[](int i) = 0;
        virtual int operator[](int i) const = 0;
        virtual vecbase create(int sz, int* values) = 0;

        friend vecbase operator+(const vecbase& a, const vecbase& b) {
        assert(a.size() == b.size());
        int* values = new int[a.size()];
        for (int i = 0; i < a.size(); ++i) {
        values[i] = a[i] + b[i];
        }
        result = create(a.size(), values);
        }
        };

        Note that I declared the operator as a friend function, rather than a member function. I prefer that approach for binary operators in case I need operators for mixed argument types. The functions in the derived classes should then change the result types of the overriden functions to the appropriate type:

        class vec2 : public vecbase {
        private:
        int val[2]
        public:
        vec2(int* values) {
        assert(values != nullptr);
        val[0] = values[0];
        val[1] = values[1];
        }
        int size() const { return 2; }
        int& operator[](int i) { return val[i]; }
        int operator[](int i) const { return val[i]; }
        // change return type on this override - it is tstill considered an override!
        vec2 create(int sz, int* values) {
        assert(sz==2);
        return vec2(values);
        }
        };

        I haven't tested this or even compiled, but this should be good enough to get the idea. P.S.: just realized that I forgot to release the values array - that one will be a bit tricky, maybe you need additional pure virtual helpers to create and release a sufficiently large initialization array... P.P.S.: I just realized this could be done much easier - all you need is a pure virtual function that performs the addition, and creates the resulting object as well:

        class vecbase {
        ...
        virtual vecbase add(const vecbase&, const vecbase&) = 0;

        friend vecbase operator+(const vecbase& a, const vecbase& b) {
        return add(a, b);
        }
        };
        class vec2 : public vecbase {
        ...
        public:
        vec2 add(const vecbase&, const vecbase&) { ... }
        };

        Of course, that defeats the purpose of implementing the operator in the base class - but then you can't really do that without virtual helper functions anyway.

        GOTOs are a bit like wire

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups