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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:29:59+00:00 2026-05-24T01:29:59+00:00

In Scott Meyers’s Effective C++ , item 18 Make interfaces easy to use correctly

  • 0

In Scott Meyers’s Effective C++, item 18 Make interfaces easy to use correctly and hard to use incorrectly, he mentioned the null shared_ptr:

std::tr1::shared_ptr<Investment> pInv(static_cast<Investment*>(0), getRidOfInvestment)

and a vogue assignment operation

pInv = ...     //make retVal point to the correct object

In which case one may need to create a null shared_ptr and do assignment later? Why not just create the shared_ptr whenever you have the resources (raw pointer)?

Since Scott Meyers did not show the complete assignment in the previous example, I thought the shared_ptr’s assign operator is overloaded that one can do this:

pInv = new Investment;    // pInv will take charge of the pointer
                          // but meanwhile keep the delete function it already had

But I tried with boost‘s implementation it doesn’t work this way. Then what is the sense to have null shared_ptr?

I am almost sure that I am missing something here, someone help me out of it please.

ps. more about the initialization and assignment of a shared_ptr

#include <boost/shared_ptr.hpp>

int main(int argc, char *argv[])
{
    boost::shared_ptr<int> ptr1(new int);
    boost::shared_ptr<int> ptr2;
    ptr2.reset(new int);
    boost::shared_ptr<int> ptr3 = new int;

    return 0;
}

this example can not be compiled by g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 and the latest boost:

sptr.cpp: In function ‘int main(int, char**)’:
sptr.cpp:8:39: error: conversion from ‘int*’ to non-scalar type ‘boost::shared_ptr<int>’    requested
  • 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-24T01:30:00+00:00Added an answer on May 24, 2026 at 1:30 am

    There is no need to use that hack to get a null (empty) shared_ptr. Simply use the default constructor:

    std::shared_ptr<Investment> pInv; // starts null
    

    To assign a pointer to a shared_ptr, either do it at construction time:

    std::shared_ptr<Investment> pInt(new Investment);
    // not allowed due to explicit annotation on constructor:
    // std::shared_ptr<Investment> pInt = new Investment;
    

    Or use the .reset() function:

    pInt.reset(new Investment);
    

    It’s possible that the author of that article may have intended to provide a custom deleter (getRidOfInvestment). However, the deleter function is reset when .reset() is called, or when otherwise the inner pointer is changed. If you want a custom deleter, you must pass it to .reset() upon creation of the shared_ptr.

    One pattern you might want to use to make this more foolproof is a custom creation function:

    class Investment {
    protected:
      Investment();
      // ...
    public:
      static shared_ptr<Investment> create();
    };
    
    shared_ptr<Investment> Investment::create() {
      return shared_ptr<Investment>(new Investment, getRidOfInvestment);
    }
    

    Later:

    shared_ptr<Investment> pInv = Investment::create();
    

    This ensures you will always have the correct destructor function attached to the shared_ptrs created from Investments.

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

Sidebar

Related Questions

According to Scott Meyers, in his Effective STL book - item 46. He claimed
I am reading Scott Meyers Effective C++ book. It was mentioned that there are
In Effective C++ (3rd edition), Scott Meyers, in Item 31, suggests that classes should
recently I've been reading through Scott Meyers's excellent Effective C++ book. In one of
Both Marshall Clines' C++ FAQ Lite and Scott Meyers' Effective C++ suggest using functions
I am reading at the time the "Effective C++" written by Scott Meyers and
Scott Meyers in Effective C++ points at the ability to do e.g. matrix operations
I'm reading Scott Meyers' Effective C++ . He is talking about traits classes, I
From Effective C++ by Scott Meyers: template<typename T, std::size_t n> class SquareMatrix: private SquareMatrixBase<T>
This is a follow-up to my question from yesterday . I have Scott Meyers'

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.