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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:41:47+00:00 2026-06-10T04:41:47+00:00

Simply put, how do you create an interface in C++ for a single level

  • 0

Simply put, how do you create an interface in C++ for a single level of inheritance (for simplicity and didactic reasons)? I saw some code that wasn’t using polymorphism, but where the base class contained a virtual abstract method (virtual void TheMethod() = 0).

Now a class was derived from this abstract class with the pure virtual method, but in the subsequent code, instances of the derived class were used without dynamic allocation at all.

Is this the correct way to do it? What about the overhead inferred by the usage of a polymorphic design?

I presume this is out of the question.. this looks more like hiding/ghosting the base method, even if that method is a pure virtual one.

Later edit: thanking all the people that managed to provide some good answers, I’d like to underline a critical error that arose from the usage of “dynamic allocation” with the meaning of emphasizing this object creation possibility as the only one compatible with polymorphism. It is quite clear that it is not the only way (but maybe the most common?) to make use of this run-time call behavior, but to further clarify my original question:

Is there a way to force a programmer to implement a method without using pure virtual methods? My perhaps unjustified concern is whether or not having opened the gate to polymorphic design is also a bit heavy on the performance side (talking thousands of such calls per second to the method in discussion).

Even later edit: making the base have a protected constructor means it cannot be instantiated directly (apart from using factories or other friendly means) and this could solve compensate for one of the effects a pure virtual methods induces. But how to make sure any derived class still provides its own method implementation? If the maybe exaggerated concern of having an associated vtable is really not that big of a deal, I’ll stick to using the pure virtual method (since SFINAE the curiously recurring template pattern is more difficult to read and understand by people that are not at least intermediate C++ programmers – like me 🙂 ).

  • 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-10T04:41:49+00:00Added an answer on June 10, 2026 at 4:41 am

    Yes, as others have stated you basically use classes with pure virtual (abstract) member functions and no data members. When implementing this interface, naturally you have to provide these methods.

    This, on the other hand has nothing to do with dynamic allocation. Whether you have automatic objects (i.e. stack) or dynamic objects (i.e. heap) is irrelevant for how you use them, including polymorphy. Do you mean dynamic binding?

    Now, having said all that, you can implement an interface without using dynamic binding (i.e. “polymorphism”) with templates. Basically, you would use SFINAE+CRTP to check if a given member function exists by privately inheriting from a template class. Basically, your parent class (which contains no virtual members) template <typename T> class FooIface; (inherited from as class Foo : private FooIface<Foo>) would make sure T has the member function foo by trying to call it. Using meta-programming tricks you can also make sure foo has the correct type.

    But this would probably be too cumbersome and too hard to read. An abstract base class is the common approach.

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

Sidebar

Related Questions

Simply put: What is the most effective way to share / reuse code between
I am working on implementing an ISrollInfo interface for a custom control. Simply put,
Simply put: Is there any way to create non-modal JFace dialog? I've tried calling
Simply put I'm trying to sync two slideshows created using widgetkit lib in a
Simply put, I'd like to be able to convert any string to an integer,
Simply put I want what http://www.reddit.com/ and http://news.ycombinator.com/ have to the left of every
Simply put, I'm getting this error in the Product Admin of Magento after adding
The issue: Simply put, my Progress dialog continuously says downloading data rather than opening
I'm trying to show/hide( or simply put - filter ) my markers( of houses
Am I missing something completely. How can we simply put the value in definition

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.