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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:51:22+00:00 2026-06-07T03:51:22+00:00

I don’t see a reason why these don’t have an assignment operator overload for

  • 0

I don’t see a reason why these don’t have an assignment operator overload for plain old pointers of the type they’re templated to. If the goal of making smart pointers interface as close to plain old pointers as they could, then why didn’t they make an overload for the assignment operator like this?

inline std::shared_ptr<type> &operator=( const type * pointer)
{
    reset(a);
}

this way you could use them just like you would a normal pointer, like so:

std::shared_ptr<int> test = new int;

it’s not an issue at all, just wondering why they went to the trouble of just overloading a couple of operators.

Also wondering if there’s a way to overload the global assignment operator to do this, or if there’s any reason i shouldn’t.

edit: adding a response to Nawaz about his answer here for code formatting. I just wrote this test program to see if what you were saying was right:

template<class T>
class peh
{
public:
    peh() {meh = 3;}
    const peh<T> & operator=(const int * peh)
    {
    }
};

void f( peh<int> teh)
{

}

int main()
{
    int * meh = new int;

    f(meh);

    system("PAUSE");
    return 0;
}

this here errors out saying there is no usable conversion from peh<int> to int *. so why is it acceptable with std::shared_ptr<int> to int *?

  • 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-07T03:51:24+00:00Added an answer on June 7, 2026 at 3:51 am

    Also wondering if there’s a way to overload the global assignment operator to do this, or if there’s any reason i shouldn’t.

    No. Assignment operator overload must a member function.

    By the way, if you want the following functionality, then you should not talk about assignment operator, you should rather ask : why the constructor which takes raw pointer as argument is made explicit? why it is not implicit?

    //this code requires an implicit constructor, not assignment!
    std::shared_ptr<int> test = new int;  //illegal 
    

    It is illegal, but suppose for a while that this was allowed, then you would be able to call the following function passing a raw pointer as argument : such a feature would be dangerous, as the rest of the answer explains (read the comments) :

    void f(std::shared_ptr<int> test)
    {
         //code
    
    } //test will be destructed here  (when it goes out of scope)
      //if test.use_count() == 1, then the pointer which it manages 
      //will be destructed as well. (NOTE THIS POINT)
    

    Now see the dangerous part:

    int *ptr = new int;
    
    f(ptr); 
    //note that calling f is allowed if it is allowed:
    //std::shared_ptr<int> test = new int;
    //it is as if ptr is assigned to the parameter:
    //std::shared_ptr<int> test = ptr;
    
    //Question : now what happened in f()?
    //Answer : inside f(), test (the shared_ptr) will infer that no one else
    //refers to the pointer it contains, because test.use_count() == 1
    //test is obviously wrong in this case, because it cannot prove that!
    
    //DANGER
    *ptr = 10; //undefined behavior, because ptr is deleted by the shared_ptr
    

    Please read the comments. It explains each part of the code snippet above.

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

Sidebar

Related Questions

Don't they both have to convert to machine code at some point to execute
Don't ask me how but I'm in a situation where I have DCPs published
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't think my virtualhost is working correctly. This is what I have inside of
Don't these two mean the same thing, first get the value and then increment?
Don't ask why but I have the requirement to draw a border around certain
DON'T ASK WHY but... I have a regex that needs to be case insensitive
Don't know if this has been answered before. Have custom routes to users. If

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.