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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:22:38+00:00 2026-06-09T16:22:38+00:00

Please consider a pure virtual class like this: class Foo { public: virtual int

  • 0

Please consider a pure virtual class like this:

class Foo {
    public: virtual int FooBar() = 0;
};

A Java developer would call such a thing an “Interface”. Usually, you then program to that interface, e.g. (bad example, sorry):

class Bar {
    public: Foo f;
};

class Child : public Foo {
    public: int FooBar() { return 1; }
};

Bar b;
b.foo = Child();

This does obviously not work in C++, because an instance of class Foo is created at construction time (but that’s not possible, because Foo is an abstract class).

Is there any pattern for “programming to an interface” in C++?

Edit: Clarified the example, sorry.

  • 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-09T16:22:39+00:00Added an answer on June 9, 2026 at 4:22 pm

    If you need something, that points to or references an implementation of an interface (or abstract class), you can use a pointer of a reference to that interface:

    class Bar {
    public:
        Bar( Foo& af ) : f( af )
        {}
    private: 
        Foo& f;
    };
    

    If you want to use a reference Foo& f, a pointer Foo* f or a reference or pointer to a const implementation (const Foo& f of const Foo* f) or even a const pointer to const implementation (const Foo* const f) depends on your requirements.

    Use a reference if possible, for composition where the referenced implementation is passed from the outside (like in my example). Use a pointer or smart-pointer if the object is more of an aggregation and is constructed by the containing object itself.

    Update:
    As no one else mentioned it up to now, if you are going to allocate the object that implements the interface dynamically, the base class have to have a virtual destructor, or otherwise you will invoke undefined behavior, even if you use a smart pointer. And beware that smart pointer can be handy, but they are not a cure for all. You should still keep some owner hierarchy in mind or you will end up with cycles that can’t be resolved by smart pointers easily.

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

Sidebar

Related Questions

Please consider the following simple use case: public class Foo { public virtual int
please consider this code : 1)public static class MyClass 2){ 3) public static DateTime
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
Please consider this class: class A { public: //public in this example string a1;
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Please consider this class: public static class Age { public static readonly string F1
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
Please consider the following code: public class Person ( public string FirstName {get; set;}
please consider this image: I have a table like this: Age Active Men/Women -------------------------------------------------
Please consider case class Foo[A, B <: List[A]](l: B) { ... } or something

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.