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
Solved

pure virtual overloaded operators

Posted on 2001-08-10
11
607 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

839 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