Solved

pure virtual overloaded operators

Posted on 2001-08-10
11
610 Views
Last Modified: 2010-08-05
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
Comment
Question by:sumant032199
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6372616
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
 

Expert Comment

by:andrewschneider
ID: 6372667
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
 
LVL 9

Accepted Solution

by:
jasonclarke earned 100 total points
ID: 6373228
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Author Comment

by:sumant032199
ID: 6376097
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
 
LVL 9

Expert Comment

by:BeyondWu
ID: 6408543
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
 

Expert Comment

by:andrewschneider
ID: 6409016
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
 
LVL 9

Expert Comment

by:BeyondWu
ID: 6411875
class interface
{
   protected:

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

0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 6411971
sumant, which compiler are you use?
I compile it in VC, and everything is OK.
Maybe you miss '&' at the end of interface?
0
 

Expert Comment

by:andrewschneider
ID: 6412602
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
 
LVL 9

Expert Comment

by:BeyondWu
ID: 6416060
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6428063
> 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question