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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:58:23+00:00 2026-06-01T06:58:23+00:00

I have something like this: struct D { virtual void operator() {…}; } struct

  • 0

I have something like this:

struct D {
    virtual void operator() {...};
}
struct D1 : public D {
    virtual void operator() {...};
}
struct D2 : public D {
    virtual void operator() {...};
}

void foo(D &d) {...};

And so this is fine, and nicely controls the life cycle of my D’s:

foo(D());
foo(D1());
foo(D2());

But I choose my D variant in multiple places, so I want a simple factory:

const D& make_D()
{
    // BAD, returning reference to temporary
    if(is_tuesday())
        return D1();
    return D2();
}

Instead of returning a reference to a temporary, I could return an object, but then I slice to the base class. Alternatively, I could return a pointer from my factory, but then the client has to delete it. Other, more complicated, solutions also impose more load on the client.

Is there a way to write something like

D& d = make_D();
foo(d);

(or even foo(make_D()))? The goal is to wrap the complexity in the various D definitions and in make_D() so that the functions like foo() and those who call those functions needn’t worry about it.

  • 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-01T06:58:24+00:00Added an answer on June 1, 2026 at 6:58 am

    The usual way is to either return a pointer or a smart pointer.

    The downside of using a pointer is letting the user manage its memory.

    If you return a smart pointer, this is no longer an issue.

    const SmartPtr<D> make_D()
    {
        if(is_tuesday())
            return SmartPtr(new D1());
        return SmartPtr(new D2());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have something like this in my code: void f(struct foo *x, struct
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I want to have something like this below: template <class T> struct Container{ public:
In our code we used to have something like this: *(controller->bigstruct) = ( struct
For example, I have a struct which is something like this: struct Test {
I have a structure something like this struct MyStructure :IFoo { } and a
So I have something like this #define HASHSIZE 1010081 static struct nlist *hashtab[HASHSIZE]; Now
In our project we have something like this: struct PointI { // methods for
I have something like this in my code typedef struct ts_fem_mesh { double **vertices;
Suppose I have something like this: struct EmptyClass{}; template<typename T1, typename T2 = EmptyClass,

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.