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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:20:54+00:00 2026-05-23T21:20:54+00:00

When using boost::scoped_ptr to hold a reference, the destructor of the derived object is

  • 0

When using boost::scoped_ptr to hold a reference, the destructor of the derived object is not called. It does when using boost::shared_ptr.

#include "stdafx.h"
#include <iostream>
#include "boost/scoped_ptr.hpp"
#include "boost/shared_ptr.hpp"

using namespace std;

class Base
{
public:
    Base() { cout << "Base constructor" << endl ; }
    ~Base() { cout << "Base destructor" << endl; }
};

class Derived : public Base
{
public:
    Derived() : Base() { cout << "Derived constructor" << endl; }
    ~Derived() { cout << "Derived destructor" << endl; }
};

int _tmain(int argc, _TCHAR* argv[])
{
    boost::scoped_ptr<Base> pb;  // replacing by shared_ptr does call Derived destructor
    pb.reset(new Derived());
    cout << "Program ends here" << endl;
}

Can you explain this? Is there a “golden rule” not to use scoped_ptr for polymorphic member variables?

  • 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-23T21:20:55+00:00Added an answer on May 23, 2026 at 9:20 pm

    The reason why it works for shared_ptr is because it implements a special constructor and a reset() method that look like this:

    template<class T>
    class shared_ptr
    {
    public:
        // ...
        // Note use of template
        template<class Y> explicit shared_ptr(Y * p);
        // ....
        // Note use of template
        template<class Y> void reset(Y * p);
        // ....
    };
    

    When this constructor or reset() is called, shared_ptr “remembers” the original type Y so that when the reference count goes to zero, it will call delete properly. (Of course p must be convertible to T.) This is explicitly stated in the documentation:

    [This constructor has been changed to a template in order to remember
    the actual pointer type passed. The destructor will call delete with
    the same pointer, complete with its original type, even when T does
    not have a virtual destructor, or is void. …]

    The scoped_ptr constructor and reset() look like this:

    template<class T>
    class scoped_ptr : noncopyable
    {
    public:
        // ...
        explicit scoped_ptr(T * p = 0);
        // ...
        void reset(T * p = 0);
    };
    

    So there’s no way for scoped_ptr to “remember” what the original type was. And when it comes time to delete the pointer, it essentially does this:

    delete this->get();
    

    And scoped_ptr<T>::get() returns a T*. So if scoped_ptr points to something that’s not a T but actually a subclass of T, you need to implement a virtual destructor:

    class Base
    {
    public:
        Base() { cout << "Base constructor" << endl ; }
        virtual ~Base() { cout << "Base destructor" << endl; }
    };
    

    So why doesn’t scoped_ptr implement a special constructor for this situation like shared_ptr does? Because “scoped_ptr template is a simple solution for simple needs”. shared_ptr does a lot of bookkeeping to implement its extensive functionality. Note that intrusive_ptr also doesn’t “remember” the original type of the pointer since it’s meant to be as lightweight as possible (one pointer).

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

Sidebar

Related Questions

#include <iostream> #include <boost/thread.hpp> using std::endl; using std::cout; using namespace boost; mutex running_mutex; struct
The following code using boost::asio will not compile: #ifndef _SERVER_H_ #define _SERVER_H_ #include Connection.h
Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;
I'm doing IPC on Linux using boost::interprocess::shared_memory_object as per the reference (anonymous mutex example).
I'm using an object to start boost thread and it has some public member
i am new to boost::shared_ptr using it the first time now. I have a
I'm trying to compile example from Boost Gzip filters page: #include <fstream> #include <iostream>
Using boost-bind , the resulting boost-function may receive more arguments than the bound object
Is it possible to have scoped macros using custom defined macros through boost wave?
Background Using boost and other similar libraries is the easiest way to find compiler

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.