Pure abstract class using operators.

Jul 14, 2011 at 4:30pm
Hi all.

I'm trying to create a pure abstract numeric class so that I can have an implementation of different number systems but they all are derived from the base class. So I have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class numberBase
{
public:
        virtual void assign(unsigned int numerator, unsigned int denominator) = 0;
        // Assignment Operators
        virtual numberBase & operator=(const numberBase& other) = 0;
        virtual numberBase & operator+=(const numberBase& other) = 0;
        virtual numberBase & operator-=(const numberBase& other) = 0;
        virtual numberBase & operator*=(const numberBase& other) = 0;

        //Arithmetic operators.
        virtual numberBase operator+(const numberBase& other) = 0;
        virtual numberBase operator-(const numberBase& other) = 0;
        virtual numberBase operator*(const numberBase& other) = 0;

        // Conditional Operators.
        virtual bool operator==(const numberBase& other) = 0;
        virtual bool operator!=(const numberBase& other) = 0;
        virtual bool operator>(const numberBase& other) = 0;
        virtual bool operator<(const numberBase& other) = 0;
        virtual bool operator>=(const numberBase& other) = 0;
        virtual bool operator<=(const numberBase& other) = 0;
};


but when I try to compile this with a derived class, I get

In file included from numbers/probFP.h:2,
from numbers/probFP.cpp:1:
numbers/numberBase.h:16: error: invalid abstract return type for member function 'virtual numberBase numberBase::operator+(const numberBase&)'
numbers/numberBase.h:6: note: because the following virtual functions are pure within 'numberBase':
numbers/numberBase.h:8: note: virtual void numberBase::assign(unsigned int, unsigned int)
numbers/numberBase.h:10: note: virtual numberBase& numberBase::operator=(const numberBase&)
numbers/numberBase.h:11: note: virtual numberBase& numberBase::operator+=(const numberBase&)
numbers/numberBase.h:12: note: virtual numberBase& numberBase::operator-=(const numberBase&)
numbers/numberBase.h:13: note: virtual numberBase& numberBase::operator*=(const numberBase&)
numbers/numberBase.h:16: note: virtual numberBase numberBase::operator+(const numberBase&)
numbers/numberBase.h:17: note: virtual numberBase numberBase::operator-(const numberBase&)
numbers/numberBase.h:18: note: virtual numberBase numberBase::operator*(const numberBase&)
numbers/numberBase.h:21: note: virtual bool numberBase::operator==(const numberBase&)
numbers/numberBase.h:22: note: virtual bool numberBase::operator!=(const numberBase&)
numbers/numberBase.h:23: note: virtual bool numberBase::operator>(const numberBase&)
numbers/numberBase.h:24: note: virtual bool numberBase::operator<(const numberBase&)
numbers/numberBase.h:25: note: virtual bool numberBase::operator>=(const numberBase&)
numbers/numberBase.h:26: note: virtual bool numberBase::operator<=(const numberBase&)
numbers/numberBase.h:17: error: invalid abstract return type for member function 'virtual numberBase numberBase::operator-(const numberBase&)'
numbers/numberBase.h:6: note: since type 'numberBase' has pure virtual functions
numbers/numberBase.h:18: error: invalid abstract return type for member function 'virtual numberBase numberBase::operator*(const numberBase&)'
numbers/numberBase.h:6: note: since type 'numberBase' has pure virtual functions
In file included from numbers/probFP.cpp:1:
numbers/probFP.h:19: error: invalid abstract return type for member function 'probFP probFP::operator+(const probFP&)'
numbers/probFP.h:7: note: because the following virtual functions are pure within 'probFP':
numbers/numberBase.h:10: note: virtual numberBase& numberBase::operator=(const numberBase&)
numbers/numberBase.h:11: note: virtual numberBase& numberBase::operator+=(const numberBase&)
numbers/numberBase.h:12: note: virtual numberBase& numberBase::operator-=(const numberBase&)
numbers/numberBase.h:13: note: virtual numberBase& numberBase::operator*=(const numberBase&)
numbers/numberBase.h:16: note: virtual numberBase numberBase::operator+(const numberBase&)
numbers/numberBase.h:17: note: virtual numberBase numberBase::operator-(const numberBase&)
numbers/numberBase.h:18: note: virtual numberBase numberBase::operator*(const numberBase&)
numbers/numberBase.h:21: note: virtual bool numberBase::operator==(const numberBase&)
numbers/numberBase.h:22: note: virtual bool numberBase::operator!=(const numberBase&)
numbers/numberBase.h:23: note: virtual bool numberBase::operator>(const numberBase&)
numbers/numberBase.h:24: note: virtual bool numberBase::operator<(const numberBase&)
numbers/numberBase.h:25: note: virtual bool numberBase::operator>=(const numberBase&)
numbers/numberBase.h:26: note: virtual bool numberBase::operator<=(const numberBase&)
numbers/probFP.h:20: error: invalid abstract return type for member function 'probFP probFP::operator-(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.h:21: error: invalid abstract return type for member function 'probFP probFP::operator*(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp: In member function 'probFP probFP::operator+(const probFP&)':
numbers/probFP.cpp:93: error: invalid abstract return type for member function 'probFP probFP::operator+(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:95: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:95: error: cannot declare variable 'result' to be of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:97: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp: In member function 'probFP probFP::operator-(const probFP&)':
numbers/probFP.cpp:100: error: invalid abstract return type for member function 'probFP probFP::operator-(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:102: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:102: error: cannot declare variable 'result' to be of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:104: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp: In member function 'probFP probFP::operator*(const probFP&)':
numbers/probFP.cpp:107: error: invalid abstract return type for member function 'probFP probFP::operator*(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:109: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:109: error: cannot declare variable 'result' to be of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:111: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
... etc,

So a numeric operator in C++ needs a reference to an objcet to be returned to allow chaining, but it can't do that as you can't create an instance of a pure abstract class (which will actually never be created). Is there a nicer way around this? Or should I just not use Pure virtual functions?
Jul 14, 2011 at 5:55pm
You should return a reference to your derived class. The return type is not part of a function's signature, it should work. Also you must make sure you define all the pure virtual functions the base declares or your derived class will also be pure virtual.
Jul 14, 2011 at 7:01pm
Hi LB,

Thanks for the reply... I'm not sure what you mean by 'return a reference to your derived class'. I will be having multipule derived classes from this base. The derived class declaration I have is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


#include <iostream>
#include "numberBase.h"

using namespace std;

class probFP : numberBase
{
public:
	probFP(unsigned int numberBits);
	virtual ~probFP();
	void assign(unsigned int numerator, unsigned int denominator);
	// Assignment Operators
	probFP & operator=(const probFP& other);
	probFP & operator+=(const probFP& other);
	probFP & operator-=(const probFP& other);
	probFP & operator*=(const probFP& other);

	//Arithmetic operators.
	probFP operator+(const probFP& other);
	probFP operator-(const probFP& other);
	probFP operator*(const probFP& other);

	// Conditional Operators.
	bool operator==(const probFP& other);
	bool operator!=(const probFP& other);
	bool operator>(const probFP& other);
	bool operator<(const probFP& other);
	bool operator>=(const probFP& other);
	bool operator<=(const probFP& other);

	void print();
private:
	friend ostream &operator<<(ostream &out, const probFP& other);
	int numBits;
	unsigned long currentNumber;
};


Could you maybe provide code as to the base class declaration?

Thanks
Matt
Jul 14, 2011 at 7:36pm
It looks correct...but you are right, it does not like the operators that are not compound assignment operators...

This code will not compile:
1
2
3
4
5
6
7
8
9
10
11
12
struct A
{
	virtual A operator+(const A&) = 0;
};
struct B: A
{
	B operator+(const B&);
};
B B::operator+(const B &with)
{
	return(*this);
}
However, if I inline the functions like this it compiles fine:
1
2
3
4
5
6
7
8
9
10
11
struct A
{
	virtual A operator+(const A&) = 0;
};
struct B: A
{
	B operator+(const B&)
	{
		return(*this);
	}
};
Last edited on Jul 14, 2011 at 10:17pm
Topic archived. No new replies allowed.