0

I have a base container class, which has a number of operators (=,+,-,+=,etc). It is expected that the logic of the operators will not need to be changed for the derived classes. Thus, ideally, I would like to use the base class operators for all of its derived classes without having to redefine them for each of the derived classes explicitly (with the exception of the assignment operator).

A solution that I came up with is demonstrated below based on a simple example. The solution seems to work, but I have doubts about the validity of the method for more complicated cases. Do you think it is valid to use this assignment "hack" in class B? What are the potential pitfalls of this method? Is there anything I missed? Are there easier ways of achieving the functionality that I need (i.e. using base class operators for derived classes)?

class A
{
 protected:
 int a;
 public:
 A(int ca)
 {
  a=ca;
 }
 A(const A& A1)
 {
  a=A1.a;
 }

 int geta() const
 {
  return a;
 } 
 void seta(int ca)
 {
  a=ca;
 }  
 const A& operator=(const A& A1)
 {
  a=A1.a;
  return *this;
 }

};

const A operator+(const A& A1, const A& A2)
{
 A myA(A1.geta()+A2.geta());
 return myA;
}

class B: public A
{
 public:
 B(int a): A(a) {}// ... using A::A;
 const B& operator=(const B& B1)
 {
  a=B1.geta();
  return *this;
 }
 const B& operator=(const A& B1)
 {
  a=B1.geta();
  return *this;
 }

};

int main()
{

 B myB(4);
 A myA(3);

 //need this to work
 myB=myB+myB;
 cout << myB.geta();
 myA=myA+myA;
 cout << myA.geta();

 int j;
 cin >>  j; 

}

  • In this code B serves no visible purpose, being just a thin wrapper around A with no added content. This is the reason why `myB=myB+myB` works. If your real code is like this, just get rid of B. If the real B is more reach than A in a non-trivial way, show how are you dealing with it. – n. m. could be an AI Jul 09 '13 at 16:20
  • @n.m. You are right. I realise now that this example is not representative of the real problem. I will have to resubmit the question. Thank you for your comment. –  Jul 10 '13 at 07:18
  • @user1391279 Hi, I just want to mention that [your recently deleted question](http://webcache.googleusercontent.com/search?q=cache:AmeqkRXnNHYJ:stackoverflow.com/questions/26790845/passing-ublas-subrange-to-a-function+&cd=1&hl=en&ct=clnk&gl=br&client=firefox-a) has lead to this very interesting discussion: http://stackoverflow.com/q/26793072/1000282. I would answer it, but, as you see, it's now deleted. Sorry, but I couldn't think of other way to contact you, I hope this comment doesn't get deleted by moderation before you read it. – oblitum Nov 07 '14 at 05:10

2 Answers2

0

For the example given i don't see any problems that can happen. Of cause you can improve the code, first by returning non const reference in operator=, second i think by adding += op to your class, and using it's code inside global operator+ function.

But in general i think it should work fine. As for assignment operators - as soon as you have only POD types and no pointers, or references or handles you don't really need any. It will become more complicated, however when pointers appear. You'll need to make sure you copy objects, pointed by them, or manage them some other way.

And you probably will not need to modify your operators as long as you don't add more members to derived classes, which also should take part in calculations.

Bogolt
  • 520
  • 1
  • 3
  • 9
0

In general you don't have to redefine functions in the base class for derived classes. With public inheritance your derived classes will have access to those functions and use their operation just fine. If you do want to re-define them (= operator for example), be sure you call the right one (virtual functions help in this case).

sedavidw
  • 11,116
  • 13
  • 61
  • 95