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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:34:53+00:00 2026-05-16T10:34:53+00:00

Given an abstract interface and an implementation derived from that interface, where constructors are

  • 0

Given an abstract interface and an implementation derived from that interface, where constructors are protected (creation of these objects only being available from a class factory – to implement a DI pattern), how can I make use of make_shared in the factory function?

For example:

class IInterface
{    
public:    
    virtual void Method() = 0;
};

class InterfaceImpl : public IInterface
{
public:
    virtual void Method() {}

protected:    
    InterfaceImpl() {}    
};

std::shared_ptr<IInterface> Create()
{
    std::shared_ptr<IInterface> object = std:: make_shared<InterfaceImpl>();    
    return object;
}

make_shared obviously cannot access the protected constructor in InterfaceImpl, or indeed in IInterface, giving me the following error


error C2248: 'InterfaceImpl::InterfaceImpl' : cannot access protected member declared in class 'InterfaceImpl'

So reading here (question: How to make boost::make_shared a friend of my class), I tried putting the following into the implementation class:


friend std::shared_ptr<InterfaceImpl> std::make_shared<InterfaceImpl>();

It still wouldn’t compile. So then I put another one into the IInterface class too. Still no joy. What have I done wrong here?

EDIT: Full source file used to compile, with “friend”…

#include <memory>

class IInterface
{    
public:    
    friend std::shared_ptr&lt;IInterface> Create();     
    virtual void Method() = 0;
};

class InterfaceImpl : public IInterface
{    
public:     
    virtual void Method() {}

protected:    
    friend std::shared_ptr&lt;IInterface> Create();     
    InterfaceImpl() {}    
};

std::shared_ptr<IInterface> Create()
{
    std::shared_ptr<IInterface> object = std::make_shared<InterfaceImpl>();    
    return object;
}

void main()
{
    std::shared_ptr<IInterface> i = Create();   
}
  • 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-16T10:34:54+00:00Added an answer on May 16, 2026 at 10:34 am

    With VC10 the solution you linked to doesn’t work – the construction of the instance of InterfaceImpl doesn’t happen in make_shared, but in an internal type in std::tr1::_Ref_count_obj<Ty>::_Ref_count_obj(void).

    I’d just make the Create() function a friend in your case and not use make_shared():

    class InterfaceImpl : public IInterface {
    // ...    
    protected:
        friend std::shared_ptr<IInterface> Create();
        InterfaceImpl() {}
    };
    
    std::shared_ptr<IInterface> Create() {
        return std::shared_ptr<IInterface>(new InterfaceImpl());
    }
    

    … or use a custom make_shared() implementation that you actually can befriend without relying on ugly implementation details.

    An alternative would be to use something like this pass-key-idiom:

    class InterfaceImpl : public IInterface {
    public:
        class Key {
            friend std::shared_ptr<IInterface> Create();
            Key() {}
        };
        InterfaceImpl(const Key&) {}
    };
    
    std::shared_ptr<IInterface> Create() {
        std::shared_ptr<IInterface> object = 
            std::make_shared<InterfaceImpl>(InterfaceImpl::Key());
        return object;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to create some objects that implement a given interface, where
Given the following code: public interface IExample { ... } public abstract class BaseExample:
Given these base classes and interfaces public abstract class Statistic : Entity, IStatistic {
I have interface IDoc and an abstract class that implements it named Doc. I
Given the following model: class Project(models.Model): project_name = models.CharField(max_length=255) abstract = models.TextField(blank=True, null=True) full_description
Abstract What I require is a technique, given a single, but layered Flash animation,
Hi I'm implementing a given design in java. Basically I have an abstract class
Given that the web application doesn't have su privileges, I'd like to execute a
Is there a way in C++ to construct your class such that given a
I have an abstract class called Node . It contains a constructor that takes

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.