Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8095487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:08:49+00:00 2026-06-05T21:08:49+00:00

I have a virtual class Element that forces its derived classes to have a

  • 0

I have a virtual class Element that forces its derived classes to have a == operator:

So, I have derived classes (Integer, Word) that implement that operator.

I have a class Group that basically is a list of elements.

In a function, I want to compare if an element from a group its equal to an element of a different group, so I’m using:

if(actual == lookingfor)

where both actual and lookingfor are pointers to Element…but the comparison is being made at the level of pointers, so both pointers are always different.

How can I force that the operator == from the derived class of element be used?

EDIT:

class Element 
{ 
  virtual int operator==(Elemento *)=0; 
}

class Word : public Element { ... } 

int Word::operator==(Element * element) 
{ 
  Element * ptr = element; 
  Word * wordPtr = dynamic_cast< Word * >(ptr); 
  int equal = 0; 
  if(wordPtr)
  { 
    equal = strncmp(this->ptr,wordPtr->ptr,49)==0; 
  } 
  return igual; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:08:51+00:00Added an answer on June 5, 2026 at 9:08 pm

    please not that the below implementation uses references instead of pointers as arguments to operator== defined as pure-abstract in struct Element. This is how it’s usually done and I’d recommend you to do the same in your code.

    #include <iostream>
    
    struct Element {
      virtual ~Element () {}
      virtual bool operator== (Element&) const =0; 
    };
    
    struct Integer : Element {
      bool operator== (Element&) const {
        return false; /* dummy impl */
      }
    };
    
    struct Word : Element {
      bool operator== (Element&) const {
        return false; /* dummy impl */
      }
    };
    

    It’s not possible to overload the comparision of pointers in C++, you will have to dereference your pointers so that you’ll get valid objects which you can compare in a normal manner, and your own operator== will be called.

    int
    main (int argc, char *argv[])
    {
      Element * e1 = new Integer;
      Element * e2 = new Word;
    
      if (*e1 == *e2)
        ;   
    
      delete e2; 
      delete e1; 
    }
    

    Another method is to call your operator== directly through your pointers, such as in the below example.

    if (e1->operator== (*e2))
      ;
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 base classes: class A { virtual SomeMethod () { <code> }
Here I have declared another virtual function in Derived class. #include <iostream> using namespace
Suppose we have an abstract class Element from which classes Triangle and Quadrilateral are
I have a Word model for a dictionary/thesaurus: public class Word { public virtual
my question is rather simple, if you have an pure virtual class (interface) but
I have a the following method definition in my class: virtual Calc* Compile( Evaluator*
i have situation like this: class IData { virtual void get() = 0; virtual
I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class
Do interfaces (polymorphic class solely with pure virtual functions) have a vtable? Since interfaces
I have a function X() declared as PURE VIRTUAL in base class: class Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.