Solved

pure virtual overloaded operators

Posted on 2001-08-10
11
609 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
Industry Leaders: 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!

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why isn't object file created? 6 108
object oriented programming comparison 5 94
I could not set window to top 4 63
MFC COM Server not showing  form 4 27
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

751 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