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 8629555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:50:29+00:00 2026-06-12T08:50:29+00:00

I want to make a class with a member function that takes a reference

  • 0

I want to make a class with a member function that takes a reference to another class, where both classes are derived from abstract classes. I get a compiler error that the class Container is abstract because it doesn’t implement addElem().

class Ielem
{
public:
  virtual void action() = 0;
};

class Elem: public Ielem
{
public:
  void action() {};
  void extra() {};
};

class Icontainer
{
public:
  virtual void addElem(Ielem &elem) = 0;
};

class Container: public Icontainer
{
public:
  void addElem(Elem &elem) { elem.extra(); };
};

int main(int argc, char* argv[])
{
  Elem e;
  Container c;
  c.addElem(e);

  return 0;
}

It seems like this ought to work, because any reference to an Elem is also a reference to an Ielem. It compiles if I make Container::addElem take a reference to an Ielem. But then Container::addElem() can’t call Elem::extra() unless I use dynamic_cast, which isn’t available on the embedded compiler I’m using, or a regular cast, which isn’t type safe.

Suggestions?

  • 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-12T08:50:30+00:00Added an answer on June 12, 2026 at 8:50 am

    The problem is simply that your virtual method doesn’t have the same signature as the concrete method which is intended to overload it; so the compiler sees it as a different function entirely and complains because you haven’t implemented void addElem(Ielem &elem). This is one solution, which you probably don’t want–

    class Icontainer
    {
    public:
      virtual void addElem(Elem &elem) = 0;  //Ielem -> Elem
    };
    

    It depends on all your other constraints but I think what I would do–and what seems to conform to general design guidelines, e.g. Sutter & Alexandreascu, would be to create an intermediate abstract class with the full interface–

    class Melem: public Ielem
    {
    public:
      // void action() {}; //Already have this form Ielem
      void extra() = 0;
    };
    

    and then

    class Icontainer
    {
    public:
      virtual void addElem(Melem &elem) = 0;
    };
    
    class Container: public Icontainer
    {
    public:
      void addElem(Melem &elem) { elem.extra(); }; 
         //*Now* we're implementing Icontainer::addElem
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a member variable that in a class that I want to make
I want to specialize following member function: class foo { template<typename T> T get()
I want to make a function called debug that outputs some info about objects.
I have a class member myMember that is a myType pointer. I want to
Lets say I want to make a class myClass with two slots A and
I have style sheet with a class name changebackgroundcolor i want make change in
I want to make an iostream type class. I would like to find the
I don't want to make a new instance of Form1 in the class, and
I want to use a category to make a method on the original class
I want make a bash script which returns the position of an element from

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.