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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:48:01+00:00 2026-05-25T01:48:01+00:00

I was wondering how you can do polymorphism with references, as opposed to pointers.

  • 0

I was wondering how you can do polymorphism with references, as opposed to pointers.

To clarify, see the following minimal example:

class A;

class B {
  public:
    A& a; ///////////////// <- #1
    B();
    void doStuff();
};

class A {
  public:
    virtual void doSmth() = 0;
};
void B::doStuff() {
  a.doSmth();
}

class A1 : public A {
  public:
    void doSmth() {
    }
};

B::B() : a(
    *        ////////////// <- #2
      (new A1)  /////////// <- #3
     ) {
}

This compiles and works, but as the most important point here is that a in line #1 is a reference, so in order to be able to use it polymorphically (is that an actual word?), as shown in line #3 I have to "convert a pointer to a reference" by dereferencing it.

This strikes me as a bit odd, and I was wondering if there is a better (in the sense of cleaner) way. Is it just me?

Rationale

It would be great if I didn’t need a new at all, but when declaring (!) B I have no clue how to create an instance of A1 (!) as A is a forward declaration — A1 is implemented in the same compilation unit as B. Still, is there a real need for dynamic memory allocation in this case? How would you do this?

Sorry for the slightly twofold question.

Edit

Note: B is huge (and I cannot make a template class of it), and will go out of scope precisely when the program terminates — a is small and makes two big modules talk to each other, it will be needed as long as the instance of B lives (there is only one).

Edit 2

I just realised, that since both A and B are effectively singletons, I can simply create a static instance of A1 in the compilation unit of B, avoiding dynamic memory allocation (even if there were two Bs they could easily use the same instance of A). To be fair, I did not post this as answer, but will accept the answer that prompted me to come up with this solution.

  • 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-05-25T01:48:01+00:00Added an answer on May 25, 2026 at 1:48 am

    There’s nothing odd. Polymorphisms works both for pointers and references:

    struct Base { };
    struct Derived : Base;
    
    void foo(Base &);
    
    int main() {
      Derived x;
      foo(x);    // fine
    }
    

    You’re conflating this with another issue, namely creating a reference to a dynamic object:

    T * pt = new T;
    T & rt = *pt;
    
    T & x = *new T;  // same effect
    

    Note that it’s generally very bad style to track a dynamic object only by reference, because the only way to delete it is via delete &x;, and it’s very hard to see that x needs cleaning up.

    There are two immediate alternatives for your design: 1) make a a member object in B, or 2) make a a shared_ptr<A> or unique_ptr<A> and change the initalizer to a(new A1). It all depends on whether you actually need the polymorphic behaviour, i.e. if you have other constructors for B which assign a different derived class to a other than A1.

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

Sidebar

Related Questions

I'm wondering how can I mock IO/Network classes. For example I have a class
I am wondering can qx.ui.form.TextArea be resized by mouse at runtime? just as I
i have a php code here and wondering how can this display what you
I was reading about CopyOnWriteArrayList and was wondering how can I demonstrate data race
I'm working with RoR and I was wondering how can I translate Acts As
I followed the tutorial exactly: http://github.com/binarylogic/authlogic_example But I am wondering how can I get
How can I do something like this using Expression trees : customers.Where(c => c.Name
I'm working on a multi-site CMS that has a notion of cross-publication among sites.
I have a sign-up form that has nested associations/attributes whatever you want to call
I want to be able to display my posts tags array [0] => grill

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.