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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:09:29+00:00 2026-05-16T23:09:29+00:00

If I have a class with a private construction, using boost::make_shared() to construct a

  • 0

If I have a class with a private construction, using boost::make_shared() to construct a shared_ptr of that class from within a member function of that class will issue a compiler error using gcc 4.6.

#include "boost/shared_ptr.hpp"
#include "boost/make_shared.hpp"

class Foo
{
private:
    Foo(int a){};
public:
    static boost::shared_ptr<Foo> do_foo(){ return boost::make_shared<Foo>(5); }
    friend template boost::shared_ptr<Foo> boost::make_shared<Foo>( Arg1 && arg1, Args && ... args );
}

int main()
{
    auto f = Foo::do_foo();
}

A call to Foo::do_foo will result in a compiler error.

Any thoughts?

  • 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-16T23:09:30+00:00Added an answer on May 16, 2026 at 11:09 pm

    Unfortunately, it is not specified which function actually calls the constructor in make_shared, so you cannot make that function a friend. If you have a class with a private constructor like this then you thus cannot construct an instance with make_shared.

    However, what you can do is create a derived class with a public constructor that calls the appropriate base class constructor, and make that derived class a friend (so it can call the private constructor):

    class Foo
    {
    private:  
        Foo(int a){};  
    public:  
        static boost::shared_ptr do_foo();
        friend class DerivedFoo;
    };
    
    class DerivedFoo: public Foo
    {
    public:
        DerivedFoo(int a):
            Foo(a)
        {}
    };
    
    boost::shared_ptr<Foo> Foo::do_foo(){ return boost::make_shared<DerivedFoo>(5); }
    

    If DerivedFoo is in an anonymous namespace in the .cpp file that defines do_foo then functions in other .cpp files will still not be able to construct any instances of Foo directly, and users will not be able to tell that what they have is actually a DerivedFoo.

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

Sidebar

Related Questions

I've got a class that have 3 methods public DoSth private DoItForReal private DoSthExtra
I´m using a class that encapsulates a thread_group, and have some questions about it
I have a question about the following code: class MyClass : private boost::noncopyable {
In general, if you have a class that inherits from a Thread class, and
I have the following class using boost filesystem, but encountered the problem when compiling.
If we have class A & B, and class A's constructor is private, and
I have a collection private ObservableCollection<Contact> _contacts; In the constructor of my class I
If I have: class Test { private Vector2 v; public Vector2 Velocity { get
Assume the following: we have class B, which is a private class nested inside
I have this class: Class B { private String D; private String E; }

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.