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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:44:48+00:00 2026-05-18T01:44:48+00:00

I have got some confused about shared_ptr. Say, I have classes: class foo {

  • 0

I have got some confused about shared_ptr.

Say, I have classes:

class foo {
     int _f;
};
typedef std::shared_ptr<foo> fooptr;

class bar {
    int _b;
};
typedef std::shared_ptr<bar> barptr;

class foobar : public foo, public bar {
    int _fb;
};

int main () {

    foobar *fb1 = new foobar();
    foobar *fb2 = new foobar();

    fooptr f((foo *)fb1);
    barptr b((bar *)fb2);

    return 0;
}

Because b.get() != fb2, so it should crash when the program exit? Or it is safe ?

  • 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-18T01:44:49+00:00Added an answer on May 18, 2026 at 1:44 am

    A shared_ptr<base> can safely take ownership of a derived*, even if base does not have a virtual destructor.

    However, this only works if shared_ptr knows what the most derived type of the object is when it takes ownership of it. If you were to remove the casts

    fooptr f(fb1);
    fooptr b(fb2);
    

    then you’d definitely be okay. With the casts, the shared_ptr cannot know what the most-derived type of the object is when it takes ownership of it, so the behavior is undefined, just as if you had said:

    foo* f = new foobar();
    delete f;
    

    The best thing to do is to follow the rule that “a base class destructor should be either public and virtual, or protected and nonvirtual.”

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

Sidebar

Related Questions

I got confused with Git !! I have some files which have added and
I'm writing my first location based android app, but got confused about some of
I've got a problem that I'm rather confused about. I have the following lines
I'm pretty confused on how to go about this. Say I have two columns
I got confused of some Rails' concepts like: gemset, rubygems, bundler . I have
I'm confused about MVC routes. Let's say I have this domain: poopoopants.com. I want
I have checked some tutorials but I got confused by the parameters in this
I'm new to EF and have read some articles. After reading I got confused
I have got some lists, and I want to filter elements from them. Here's
I have got some javascript code and I'd like to convert this to C#.

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.