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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:24:05+00:00 2026-06-07T22:24:05+00:00

I have an abstract class and two concrete subclasses (Store), both with a pointer

  • 0

I have an abstract class and two concrete subclasses (Store), both with a pointer to another concrete subclass which is derived from an abstract class (Factory). Below is the code for the Store. I wanted to prevent memory leaks, so I started to write the copy control. I can’t instantiate a new Factory however, because I don’t know upfront what type it will be. What is a good practice to circumvent this? I could write the copy control in the concrete Stores, but then I have duplicate code.
I also tried to work with smart pointers instead, but there I find another difficulty. The snippet myFactory = std::make_shared<AbstractFactory>(ConcreteFactoryA()); apparently creates an AbstractFactory first, and then fills it with a ConcreteFactoryA. However, as the name suggests, AbstractFactory cannot be instantiated, as the compiler is telling me. Can you use shared_ptrs with abstract classes?

Code with plain pointers:

#pragma once
#include "AbstractFactory.h"

class AbstractStore
{
public:
    // Copy control
    AbstractStore(const AbstractStore& orig) : myFactory(new AbstractFactory(orig.myFactory)) {}
    AbstractStore& operator=(const AbstractStore& orig) { return *this; } // TODO
    ~AbstractStore(void) {}
protected:
    // Constructor
    AbstractStore(void) {}
    // Data members
    AbstractFactory* myFactory;
};

class ConcreteStoreA : public AbstractStore
{
public:
    ConcreteStoreA(void) { myFactory = new ConcreteFactoryA; }
    ~ConcreteStoreA(void) {}
};

class ConcreteStoreB : public AbstractStore
{
public:
    ConcreteStoreB(void) { myFactory = new ConcreteFactoryB; }
    ~ConcreteStoreB(void) {}
};

Code with smart pointers:

#pragma once
#include "AbstractFactory.h"
#include <memory>

class AbstractStore
{
public:
    // Copy control
    AbstractStore(const AbstractStore& orig) : myFactory(orig.myFactory) {}
    AbstractStore& operator=(const AbstractStore& orig) { myFactory = orig.myFactory; return *this; }
    ~AbstractStore(void) {}
protected:
    // Constructor
    AbstractStore(void) {}
    // Data members
    std::shared_ptr<AbstractFactory> myFactory;
};

class ConcreteStoreA : public AbstractStore
{
public:
    ConcreteStoreA(void) { myFactory = std::make_shared<AbstractFactory>(ConcreteFactoryA()); }
    ~ConcreteStoreA(void) {}
};

class ConcreteStoreB : public AbstractStore
{
public:
    ConcreteStoreB(void) { myFactory = std::make_shared<AbstractFactory>(ConcreteFactoryB()); }
    ~ConcreteStoreB(void) {}
};
  • 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-07T22:24:06+00:00Added an answer on June 7, 2026 at 10:24 pm

    You are not using make_shared correctly. Use:

    std::make_shared<ConcreteFactory>();
    

    You call it without any arguments here. make_shared is not accepting a constructed object but the arguments that are forwarded to it’s constructor. In your case you would be forwarding to the copy constructor, which works poorly with abstract hierarchies. If you want copyable objects in hierarchies, use clone member functions with covariant return types.

    This will return a shared_ptr<ConcreteFactory> which will be converted to shared_ptr<AbstractFactory> in the assignment (see (9) here. Also, use constructor initializer lists and virtual destructors.

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

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
I have an abstract class for moving data from one database to another, and
I have an abstract class that defines some methods. This class has two subclasses.
I have an abstract class which implements two interfaces. Am I right in thinking
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
I have an abstract class Class. The class Subclass extends Class. The Class abstract
I have an abstract class and two sub classes that extend it. I have
I have two classes SccmAction and TicketAction which both implement interface IDelivery. These classes

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.