views:

327

answers:

6

How can I make pure virtual function a operator+(); function. wheh ı do like this in base class int operator+()=0; compiler gives error . in derive class operator+() function compiler say that derive class cannot make . because following class is abstract I know that I cannot create object of abstract classes but now I try to make derive class object.

Here is the code

#include <iostream>
using namespace std;
class ana {
    protected :
    int x;

    public :
    virtual int operator+()=0;
    virtual void operator=(ana&)=0;
    };

class baba : public ana{
    public:
    baba(int k){x=k;}
    int   operator+(baba &ali){
        int k;
        k=x+ali.x;
        return k;
    }
   void operator=(baba &ali){
       x=ali.x;
       }


    };

int main(){
    baba k(4);

    return 0;
    }
A: 

The syntax for a pure virtual function would be something like:

class X { 
public:
     virtual int operator+(X const &rhs) = 0;
};

Note, however, that you only rarely want to do this -- you typically want an overload of operator+ to be implemented as a free function, to allow conversion of the left operand (if necessary).

Jerry Coffin
This is incorrect, you cannot return an abstract class.
Kornel Kisielewicz
Fortunately hasan wants to return just an int.
@kibibu:you probably wouldn't -- but contrary to your previous comment, a pointer isn't necessary; polymorphism via references is perfectly fine. Nonetheless, the whole thing is problematic.
Jerry Coffin
@Jerry, thanks, I wasn't aware of this. I thought I knew c++ too...
kibibu
As Alex noted, this, too, misses a vital `const`.
sbi
@Jerry: Show me a sensible `operator+` that returns a reference. (Isn't that an item in _EC++_?)
sbi
@sbi: I specifically said "The syntax...", but then said, "you rarely want to do this" (and even the "rarely" is probably being generous -- I'm not sure you ever do). At least IMO, worrying about const correctness is pointless -- just don't do it.
Jerry Coffin
@Jerry: My comment about the sensible `operator+` was specifically regarding your comment to kibibu, where you didn't say most of these things. (I hadn't even seen that you are the one who posted this answer. Sorry for the confusion.)
sbi
@Jerry: Having taught C++ for a few years, I have learned that, when teaching, one needs to do it right every time. Beginners can't tell that a piece only cares about one thing (here: the silliness of `virtual`) and disregards other aspects of the code (here: constness), which shouldn't be disregarded in other contexts. Someone will fast-scan your answer, hopefully realizing that you say `virtual operator+` is rarely ever Ok, copy the code except for the `virtual`, and come back here asking why this fails to compile with a horrible error message when they have a `const` on the left side.
sbi
@sbi: I agree that an example of what to do should be exemplary in general, not just the aspect being demonstrated at the moment. This, OTOH, specifically says to use a free function instead; if somebody ignores that and uses it anyway, I doubt that adding `const` is going to help them.
Jerry Coffin
@Jerry: You haven't convinced me, but from now on we would just repeat ourselves, so I'm going to stop this.
sbi
+1  A: 

Note : Question have been updated, making this answer less valid.

If you're description is correct, you're just forgetting to use the virtual keyword to specify the operator as virtual...

class Addable{
public:
    virtual int operator+ const ( const Addable& other ) = 0; // pure virtual
    virtual int operator+ const ( int number ) = 0; // pure virtual
};
Klaim
re: my comment on the original post, how would one actual call such a beast, given a pointer `Addable * px`? My c++ is a little sketchy.
kibibu
@kibibu: That's no problem. For user-defined types, `a+b` becomes either `operator+(a,b)` or `a.operator+(b)`, depending on how the operator is overloaded. The latter is, except for the funny function name which allows the infix syntax, no different from `a.f(b)`.
sbi
I still believe that designs involving virtual operators are nonsensical.
sbi
This depends on what you are trying to accomplish. For example, if you were to be implementing a dynamic-type script language such as Lua, one might suggest that polymorphic operators is right up that street.
DeadMG
Never mind, I missed a response by @Jerry Coffin elsewhere. I didn't realize virtual function lookup happened through references as well as through pointers.
kibibu
@sbi:I'm not sure I'd go *quite* that far. Just for one example, I can imagine a class `functor` with `virtual void operator()()=0;` as making sense under some circumstances.
Jerry Coffin
@Jerry: Yes, you're right, I probably overshot, some other operators (unary prefix `*`, for example) might also be fine. But `operator+` is used with _values_, and value semantics and `virtual` just don't fit.
sbi
As Alex noted, this, too, misses a vital `const`.
sbi
+3  A: 

Your vague mentions of code are essentially impossible to follow. Answering your question "How can I make pure virtual function a operator+(); function", there's absolutely no secret to it, e.g. consider the following trivial program:

#include <iostream>

class base {
public:
  virtual int operator+(int) const = 0;
};

class deriv: public base {
public:
  int operator+(int) const {return 23;}
};

int main() {
  deriv d;
  base& b = d;
  std::cout << b + 15 << std::endl;
  return 0;
}

This compiles and runs just fine and emits 23 as expected. Whatever it is that you're doing wrong, obviously it must therefore be different from this (and probably not connected to the specific issue of having an operator overload be pure virtual).

Edit: (as per comments, added const to the method just in case you want to call it w/a const base& -- note that other answers have also omitted this const; and, also per comments):

If you want to be able to also do 15 + b, just add a free-standing function for that very purpose, say just before main:

inline int operator+(int i, const base&b) {
    return b + i;
}
Alex Martelli
This has the obvious disadvantage that `15 + b` will not work.
anon
sbi
@sbi, OK, let me edit the question to make it more novice friently (I notice the same lack of the "obvious const" in all other answers, btw).
Alex Martelli
@Alex: Good. Up-voted.
sbi
+1  A: 

If you are searching for a standard operator+() implementation, then unfortunately that is impossible:

class X { 
public:
     virtual X operator+(X const &rhs) = 0;
};

This code cannot compile, because the compiler cannot return an abstract X class by value.

Kornel Kisielewicz
Could return X* or a smartpointer to X. Hell, he could write his own smart pointer class that's really just a std::shared_ptr and solve this that way.
DeadMG
As Alex noted, this, too, misses a vital `const`.
sbi
A: 
hasan
Did you just ignore all the comments above? "int operator+(baba " is NOT overriding "virtual int operator+()=0;" - they have different arguments.
Colin
@Colin: I think he's added an answer by mistake instead of editing the question. This is the code we've been asking for.
Troubadour
@hasan: I've added the code to your question. You can delete this answer.
Troubadour
Thanks a lot @Colin
hasan
+1  A: 
andand
As Alex noted, this, too, misses a vital `const`.
sbi
@sbi: It should be fixed now.
andand