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

  • Home
  • SEARCH
  • 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 7862525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:09:41+00:00 2026-06-02T23:09:41+00:00

I made some testing with shared_ptr,and i can’t think out the matter below.I just

  • 0

I made some testing with shared_ptr,and i can’t think out the matter below.I just started to learn the boost library. Is there anybody can tell me the reason?

#include <boost\shared_ptr.hpp>
#include <iostream>

class A 
{
public:  
    virtual void sing()
    {
        std::cout<<"A";
    }
protected:  virtual ~A() {};

};

class B : public A 
{
public:  
    virtual void sing() 
    {   
        std::cout << "B"; 
    }
    virtual ~B() {};
};


int foo()
{   
    boost::shared_ptr<A> pa(new B());
    pa->sing();

    delete static_cast<B*>(pa.get());

    delete pa.get(); //this line has a problem error C2248: “A::~A”: can't access protected memmber(declared in class“A")   
    return 0;
}

int main()
{
    foo();
    return 0;
}

but it can be compiled when that line is commented out. Surely it doesn’t mean that the shared_ptr will delete the pointer internally maintained out of the main function, just like what i did. Is there any difference between the pointer returned by pa.get() and the pointer internally maintained?

  • 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-02T23:09:46+00:00Added an answer on June 2, 2026 at 11:09 pm

    I believe that delete is called during destruction of the shared_ptr on the type of the pointer passed into the constructor. Have a look at the constructor here:

    http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#constructors

    So when your pa goes out of scope, B::~B( ) is called rather than the destructor of the type contained – A::~A ( which would be impossible because it’s declared protected).

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

Sidebar

Related Questions

I made some Eclipse plugin and update site on Indigo(Eclipse v3.7). I can debug
I have made some repetitive operations in my application (testing it), and suddenly I’m
I've made a simple dummy server/dummy client program using IOCP for some testing/profiling purpose.
I just had to produce a long xml sequence for some testing purpose, a
I am... kind of a beginner on unit testing. I just read some Unit
I'm working with chaotic attractors, and testing some continuous-> discrete equivalences. I've made a
I'm doing some testing and optimizing on a JavaScript function I made. I notice
I need to do some testing for a website made in php/mysql and wanted
I made some changes to an open source project without taking time to create
I made some code, for understanding the concept/basic of pointer: int a=1; int *b=&a;

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.