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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:05:14+00:00 2026-05-28T00:05:14+00:00

(Using Visual Studio 2010) I’m trying to create a shared_ptr of an existing class

  • 0

(Using Visual Studio 2010) I’m trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). This class takes a non-const pointer to another object, it’s empty parameter constructor is private.

class Foobar {
public:
    Foobar(Baz* rBaz);

private:
    Foobar();
}

When I try to create a shared_ptr to it, things don’t go well:

Baz* myBaz = new Baz();
std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(new Foobar(myBaz));

On VS2010, this gives me

error C2664: 'Foobar::Foobar(const Foobar &)' : cannot convert parameter 1 from 'Foobar *' to 'const Foobar &'
3>          Reason: cannot convert from 'Foobar *' to 'const Foobar'

For some reason it appears to be calling the copy constructor of Foobar instead of the constructor that takes a Baz*.

I’m also not sure about the cannot convert from 'Foobar *' to 'const Foobar' part. My best interpretation is that my templated-type of shared_ptr<Foobar> is wrong. I made it shared_ptr<Foobar*> but this seems wrong, all examples I’ve seen don’t make the type a raw pointer.

It seems that making everything shared_ptr<Foobar*> compiles properly, but will that prevent the Foobar object from getting deleted properly when all shared_ptr‘s go out of scope?

Edit: This seems related, but I’m not using Boost: boost make_shared takes in a const reference. Any way to get around this?

Edit2: For clarity, if you’re wondering why I’m using make_shared(), in my actual code the sharedFoo variable is a class member of a third class (independent of Foobar and Baz).

  • 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-28T00:05:14+00:00Added an answer on May 28, 2026 at 12:05 am

    That should be;

    std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(myBaz);
    

    …since make_shared constructs the actual object for you by running the constructor.

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

Sidebar

Related Questions

Using Visual Studio 2010 C++ with GMock. Trying to create a stub object for
I'm using the Visual Studio 2010 RC for .NET 4.0 and I'm trying to
Using Visual Studio 2010 when creating a wcf service the template creates a class
I'm using Visual Studio 2010, and in my C++/CLI project there are two Code
I'm currently using Visual Studio 2010 Professional, and noticed the ability to create Word
I am using Visual Studio 2010 to create a Silverlight 4 application. I set
Using Visual Studio 2010, when I add the four IronRuby references to an existing
Using Visual Studio 2010 I created an Office 2010 Word add-in project that has
Im using Visual Studio 2010 Ultimate and Im trying to convert some images using
I am using visual studio 2010 express C#. When ever i try to create

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.