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

  • Home
  • SEARCH
  • 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 67429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:15:30+00:00 2026-05-10T19:15:30+00:00

OK, I have a somewhat complicated system in C++. In a nutshell, I need

  • 0

OK, I have a somewhat complicated system in C++. In a nutshell, I need to add a method to a third party abstract base class. The third party also provides a ton of derived classes that also need the new functionality.

I’m using a library that provides a standard Shape interface, as well as some common shapes.

class Shape {     public:         Shape(position);         virtual ~Shape();          virtual position GetPosition() const;         virtual void SetPosition(position);          virtual double GetPerimeter() const = 0;      private: ... };  class Square : public Shape {     public:         Square(position, side_length);     ... };  class Circle, Rectangle, Hexagon, etc 

Now, here’s my problem. I want the Shape class to also include a GetArea() function. So it seems like I should just do a:

class ImprovedShape : public virtual Shape {     virtual double GetArea() const = 0; };  class ImprovedSquare : public Square, public ImprovedShape {     ... } 

And then I go and make an ImprovedSquare that inherits from ImprovedShape and Square. Well, as you can see, I have now created the dreaded diamond inheritance problem. This would easily be fixed if the third party library used virtual inheritance for their Square, Circle, etc. However, getting them to do that isn’t a reasonable option.

So, what do you do when you need to add a little functionality to an interface defined in a library? Is there a good answer?

Thanks!

  • 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. 2026-05-10T19:15:31+00:00Added an answer on May 10, 2026 at 7:15 pm

    We had a very similar problem in a project and we solved it by just NOT deriving ImprovedShape from Shape. If you need Shape functionality in ImprovedShape you can dynamic_cast, knowing that your cast will always work. And the rest is just like in your example.

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

Sidebar

Related Questions

I'm creating a System-Tray only application. It's somewhat complicated to have the icon without
I have a somewhat complicated branching structure at work (at least for me). It
I have a somewhat messily-formatted Objective-C code base. Is there a way to have
Ok SQL and Oracle gurus I have a somewhat complicated query that I'm trying
This is somewhat complicated to explain, so please bear with me. I have an
I have a somewhat complicated Makefile which runs perl scripts and other tools and
I have somewhat interesting development situation. The client and deployment server are inside a
Does VS2008 have somewhat C++0x standard support? DUPLICATE Visual Studio support for new C
I have several somewhat separate programs, that conceptually can fit into a single project.
I am learning about SOAP implementation and have become somewhat confused regarding the appropriate

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.