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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:22:34+00:00 2026-05-25T17:22:34+00:00

the idea is that I want a class which is wrapped by std::shared_ptr ,

  • 0

the idea is that I want a class which is wrapped by std::shared_ptr, can still be used
just like they weren’t a pointer, for example the operator= which was defined in my class
can still be used after my class is wrapped by std::shared_ptr.

for example

template <class Ty> class shared_ptr_proxy : public std::shared_ptr<Ty> {
public:
    template<class Other> shared_ptr_proxy& operator=(const Other& rhs)
    {
        (*this->get()) = rhs;
        return *this;
    }
    template<class Other> explicit shared_ptr_proxy(Other * ptr) 
        : std::shared_ptr<Ty>(ptr){};
};

// usage :
shared_ptr_proxy<float> obj = shared_ptr_proxy<float>(new float);
obj = 3.14;

its work, but is there a way that i don’t need to create shared_ptr_proxy or
inheriting a class from std::shared_ptr ?

and

if I do like this, is there a caveat that i should take care of?

  • 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-25T17:22:35+00:00Added an answer on May 25, 2026 at 5:22 pm

    It depends on what you want the proxy for. A full proxy might make it look entirely like you had the value, so you’d provide the conversion operators.

    In such case, it might not be a good idea to inherit from shared_ptr, though, because you might be inheriting functions that you want to rely on the implicit conversions instead.

    Compare how sorting orders the items:

    #include <memory>
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    template <class Ty> class shared_ptr_proxy   {
        std::shared_ptr<Ty> ptr;
    public:
        template<class Other> explicit shared_ptr_proxy(Other * p) 
            : ptr(std::shared_ptr<Ty>(p)){};
    
        template<class Other> shared_ptr_proxy& operator=(const Other& other)
        {
            *ptr = other;
            return *this;
        }
    
        operator Ty& () { return *ptr; }
        operator const Ty& () const { return *ptr; }
    };
    
    int main()
    {
        std::vector<shared_ptr_proxy<int> > vec {
            shared_ptr_proxy<int>(new int(10)), 
            shared_ptr_proxy<int>(new int(11)), 
            shared_ptr_proxy<int>(new int(9))
        };
        vec.back() = 8;  //use assignment
        std::sort(vec.begin(), vec.end());  //sort based on integer (not pointer) comparison
        for (unsigned i = 0; i != vec.size(); ++i) {
            std::cout << vec[i] << ' ';  //output stored values
        }
    }
    

    #include <memory>
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    template <class Ty> class shared_ptr_proxy : public std::shared_ptr<Ty>   {
    public:
        template<class Other> explicit shared_ptr_proxy(Other * p) 
            : std::shared_ptr<Ty>(p){};
    
        template<class Other> shared_ptr_proxy& operator=(const Other& other)
        {
            *this->get()= other;
            return *this;
        }
    
        operator Ty& () { return *this->get(); }
        operator const Ty& () const { return *this->get(); }
    };
    
    int main()
    {
        std::vector<shared_ptr_proxy<int> > vec {
            shared_ptr_proxy<int>(new int(10)), 
            shared_ptr_proxy<int>(new int(11)), 
            shared_ptr_proxy<int>(new int(9))
        };
        vec.back() = 8;  //the only thing that works
        std::sort(vec.begin(), vec.end());  //sort based on pointer values
        for (unsigned i = 0; i != vec.size(); ++i) {
            std::cout << vec[i] << ' ';  //outputs addresses
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've gotten used to the idea that if I want/need to use alpha-trans PNGs
I'm having problems with a DataTable line, the idea is that I want to
Simple idea: I have two images that I want to merge, one is 500x500
I have a php calendar at http://idea-palette.com/aleventcal/calendar.php . I want the days that are
I want to implement a class which its fields could change (add new fields)
I want to create wrapper class, which will enable keys duplicates while default hash
I have a class, which is just a wrapper over a list, i.e., public
I have an enum value as a member of a class which I want
I have a class that I want to use to store properties for another
Can I use scala List in Java, like : import scala.collection.immutable.List; class HelloScalaList {

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.