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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:31:55+00:00 2026-05-15T08:31:55+00:00

I’m not sure if the question title is accurate… Let me start by explaining

  • 0

I’m not sure if the question title is accurate… Let me start by explaining my original simple scenario, and then move on to explain what would I like to do, but can’t.

Originally, I had something like:

class Operand;

Operand genOperandA() { ...; return Operand(); }
Operand genOperandB() { ...; return Operand(); }
... // more operand-generation functions

typedef Operand (*OpGen)();

// Table of function pointers
static const OpGen generators[] =
{
    genOperandA,
    genOperandB,
    ...
};

// Function to do some operation on the operand
void operate(Operand& op);

...

// Example call
operate(generators[1]());

So far so good (I think). However, there are now several derived operand types, e.g. class RegisterOperand : public Operand. I have new, dedicated genOperand functions that ideally would return instances of the derived types. But I can’t do this:

Operand genOperandC() { ...; return RegisterOperand(); }

and I can’t do this:

RegisterOperand genOperandC() { ...; return RegisterOperand(); }

static const OpGen generators[] = 
{
    ...
    genOperandC,
};

However, I know this would work if I were to return reference or pointer types, so the only option I currently have is something like:

Operand *genOperandC() { ...; return new RegisterOperand(); }

which now requires explicit cleanup which wasn’t necessary originally.

Any alternatives I haven’t considered?

  • 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-15T08:31:55+00:00Added an answer on May 15, 2026 at 8:31 am

    There might be other designs that doesn’t require you to use pointers, but if you need or want to go this way, this might interest you.


    If returning a pointer is a problem (because of the need to “clean-up” things), you definitely should consider using smart pointers as return type.

    Here is an example of your factory method with smart pointers:

    boost::shared_ptr<Operand> genOperandC()
    {
      return boost::shared_ptr<Operand>(new RegisterOperand());
    }
    

    This way, you won’t have to call delete manually: it will be done by the destructor of boost::shared_ptr<Operand> for you when required.

    If afterwards you need to cast the resulting pointer, boost provides casting functions as well:

    boost::shared_ptr<Operand> op = genOperandC();
    
    boost::shared_ptr<RegisterOperand> rop =
      boost::dynamic_pointer_cast<RegisterOperand>(op);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.