Solved

pure virtual overloaded operators

Posted on 2001-08-10
11
602 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now