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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:40:10+00:00 2026-05-27T18:40:10+00:00

Having in mind the abstract factory pattern, imagine that you have a class hierarchy

  • 0

Having in mind the abstract factory pattern, imagine that you have a class hierarchy where concrete factories override a createButton*s* virtual method, needing to return a wider array of buttons. What could be an elegant workaround to resolve this, as C++ supports only covariant return types? dynamic_cast?

A modified version of GoF’s example to fit my requirement:

class Button {
public:
        virtual void paint() = 0;
        virtual ~Button(){
        }
};

class WinButton: public Button {
public:
        void paint() {
                cout << "I'm a WinButton";
        }
};

class OSXButton: public Button {
public:
        void paint() {
                cout << "I'm an OSXButton";
        }
};

class GUIFactory {
public:
        virtual Button * createButtons() = 0;
        virtual ~GUIFactory(){
        }
};

class WinFactory: public GUIFactory {
public:
        WinButton* createButtons() {
                return new WinButton[2];
        }

        ~WinFactory(){
        }
};

class OSXFactory: public GUIFactory {
public:
        OSXButton* createButtons() {
                return new OSXButton[2];
        }

        ~OSXFactory(){
        }
};

And the application skeleton:

Application(GUIFactory * factory) {
                Button* buttons = factory->createButtons();
                for(...) {...}
        }
  • 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-27T18:40:11+00:00Added an answer on May 27, 2026 at 6:40 pm

    It cannot be done. Ask yourself, will the caller delete or delete[] it? How will they know how many elements are in the arrays? There’s no way to solve either of these problems with a raw pointer return. So the simple answer to your question is that this cannot be done, nor should you even want to. The callee needs to know the return type- whether it’s one button or many, and how to clean them up when he’s done.

    You can’t even access the non-first element without invoking Undefined Behaviour.

    A Factory is supposed to construct individual objects. If you need more than one, call the factory function more than once.

    Even if you magically had code that could deal with more than one button at once when they were expecting just one (wtf?), you need to safely return more than one. That means std::vector<smart_pointer<Button>>.

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

Sidebar

Related Questions

Having in mind that each webapp has its own separate database (but all databases
I currently have the following: public abstract class CharacterClass { public abstract Attribute FirstAttributeBonus
Having a project with following requirements in mind. data reading intensive application. 100 max
Having this route: map.foo 'foo/*path', :controller => 'foo', :action => 'index' I have the
I am having a mind-freeze here, but I cant seem to find the equivalent
Maybe I am having a mind block here but I cant seem to get
Would anyone mind having a look at these bits of code and see if
I want to create a class (static?) that simply maps a name to a
I have about 20 simple C++ classes that represent various probability distributions. I would
I have access to some images that can be gathered like this: http://www.example.com/imageGetter.php?a=0001 If

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.