Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 617
  • Last Modified:

pure virtual overloaded operators

Hi,

I wrote following abstract class
class interface
{
      protected:

      virtual boolean operator < ()=0;
      virtual boolean operator == ()=0;
      virtual boolean operator > ()=0;
};

when I inherited it in a derived class it says
above virtual functions have less than required parameters. One more para is needed, thats true, but which one? I am going to inherit the abstract class in many custom classes.

0
sumant032199
Asked:
sumant032199
  • 4
  • 3
  • 2
  • +2
1 Solution
 
jkrCommented:
Well, it usually should read

class interface
{
     protected:

     virtual boolean operator < (const interface&)=0;
     virtual boolean operator == (const interface&)=0;
     virtual boolean operator > (const interface&)=0;
};

0
 
andrewschneiderCommented:
As above but normally you'd define comparison operators outside the interface so that you get type coercion on both the LHS and RHS of the operator. You're comparison operator functions would then delegate to member functions.
0
 
jasonclarkeCommented:
BTW, the concept here may be flawed... defining abstract comparison operators like this on a base class is often not the right thing to do.

Think about what might happen if the LHS & RHS are both derived from interface but are of different types.

The member function for the object on the LHS will be called - but what should it do with the object on the RHS whose type it does not know about?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sumant032199Author Commented:
hello jasonclarke, jkr,

How does the problem occur If I we use abstract class
as shown by jkr? Actually I am not able to instantiate
any class object after inheriting the interface, even though the class has required overloaded operators. Whats going wrong? Compiler says: can't have objects of abstract class 'myclass'. (It does not have any pure virtual functions)
0
 
BeyondWuCommented:
Change the interface& to interface*.
class interface
{
    protected:

    virtual boolean operator < (const interface*)=0;
    virtual boolean operator == (const interface*)=0;
    virtual boolean operator > (const interface*)=0;
};
0
 
andrewschneiderCommented:
BeyondWu, that isn't going to work.

interface a;
interface b;

a < b will not invoke operator<(interface *) since b is not a pointer.

sumant, If the compiler says myclass is abstract then it is. Make sure each pure virtual method it inherits is defined in myclass with exactly the same return types, argument types and constness.
0
 
BeyondWuCommented:
class interface
{
   protected:

   virtual boolean operator < (interface&)=0;
   virtual boolean operator == (interface&)=0;
   virtual boolean operator > (interface&)=0;
};

0
 
BeyondWuCommented:
sumant, which compiler are you use?
I compile it in VC, and everything is OK.
Maybe you miss '&' at the end of interface?
0
 
andrewschneiderCommented:
BeyondWu: Those arguments should be const and really the methods should be const. Otherwise you're saying that a comparison can effect both the object state and the argument's state.
0
 
BeyondWuCommented:
andrewschneider: Yes, in the logical, it should be const. but it only protected by compiler, it's so weak that the user still can change the content of a const object use type-converting.
So, I think if the style("virtual boolean operator < (const interface&)=0") can't be compiled, maybe we can remove the const definition.
But on my computer, I can compile all the style.
0
 
jasonclarkeCommented:
> Compiler says: can't have objects of abstract
> class 'myclass'. (It does not have

any pure virtual functions) I suspect maybe you are trying something like this in your derived class:

class d : public interface
{
    virtual boolean operator == (const d&);
};

but this won't work, you must have *exactly* the same arguments in your own code, so you must actually implement the operator == with an argument of type interface ... and that is part of the problem in this kind of hierachy.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now