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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:26:32+00:00 2026-06-16T02:26:32+00:00

Preface: I want to be able to store a (abstract) base class reference from

  • 0

Preface: I want to be able to store a (abstract) base class reference from an object of a derived (concrete) class type so it can be returned later and casted to the same derived (concrete) class without resorting to pointers and constantly having to worry about ownership, memory leaks, and dangling pointers/bad references.

Using the hierarchy below, the attempted constructor call: A(/* Other variables */, Bar(/* Bar variables */)); fails with `Warning 3 warning C4239: nonstandard extension used : ‘argument’ : conversion from ‘Bar’ to ‘Foo&’ (Visual Studio 2010 SP1)

Changing the class O to contain a Foo* _foo and the constructor initialization to /*...*/, _foo(&foo) causes a dangling pointer as the temporary object is destroyed when the constructor finishes.

Is there a way to pass a temporary object to a class that expects a reference and not have the code go bonkers?

`

/* ABSTRACT BASE CLASS */
class Foo {
    //...
};

/* DERIVED, CONCRETE CLASS */
class Bar : public Foo {
    //...
};


class O {
    O(/* Other variables */, Foo& foo) : /* Other member variable initializations */, _foo(foo) { }

    //...Other member variables here...
    Foo& _foo;
    friend class A;
};

class A {
    public:
        A(/* Other individual variables used to fully construct object 'O' */, Foo& foo) : /*...*/, _foo(foo) { }
    private:
        O _o

};
  • 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-16T02:26:35+00:00Added an answer on June 16, 2026 at 2:26 am

    Is there a way to pass a temporary object to a class that expects a reference and not have the code go bonkers?

    No. The very reason why you do not have to worry that much about ownership when you deal with references is that the ownership is dealt with some place else – specifically, at the level owning the variable being referenced.

    A reference becomes invalid the moment the object being referenced goes out of scope (yes, references can be dangling too). The object being referenced must stay around as long as the reference to it is in active use. That is why you cannot make temporaries play nicely with references: temporaries go out of scope very quickly, rendering useless the references to them.

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

Sidebar

Related Questions

To preface my question, couple things to note. I don't want to store said
I want to preface this by saying I believe the Right™ Way to handle
I'm using a list of Actions to store an undo history for an object.
I want to preface this with 'I am a complete noob' when it comes
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
Let me preface this by saying I am really new to C++. I want
Preface : In my application, I store raw WAV data in the database as
I want to preface this question by stating that I am fairly new to
Let me preface this by saying that I know that I can use HTML.Raw()
I want to preface, I didn't write this. I inherited it earlier today, and

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.