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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:50:49+00:00 2026-06-12T18:50:49+00:00

Can you have a parent class shared pointer return type of a function and

  • 0

Can you have a parent class shared pointer return type of a function and then return a new child class without it being a shared pointer? I’m not sure how shared pointers work in these situations, do they act like a regular pointer? Here is my example:

BaseEventPtr Actions::getEvent(const std::string& nodeName)
{
    if(asLowerCaseString(nodeName) == "action")
        return new ActionEvent(&m_interface);

    return nullptr;
}

ActionEvent is the subclass of BaseEvent in this situation.

Cheers!

  • 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-12T18:50:50+00:00Added an answer on June 12, 2026 at 6:50 pm

    If BaseEventPtr is a smart pointer it should be OK.

    Basically the shared pointer calls delete on the real pointer when there are no more references. If the base class has a virtual destructor defined, delete calls the proper subclass’ destructor.

    For example:

    class NonVirtualBase {};
    class NonVirtualSubclass: public NonVirtualBase {};
    
    shared_ptr<NonVirtualBase> ptr( new NonVirtualSubclass() ); // NOT OK!
    
    class VirtualBase
    {
        virtual ~VirtualBase() {}
    };
    
    class VirtualSubclass: public VirtualBase {};
    
    shared_ptr<VirtualBase> ptr( new VirtualSubclass() ); // OK
    

    This applies to normal (naked) pointers as well. That’s why as a general rule, if a class may serve as a base class in the future, it should be defined with a virtual destructor (even if empty).

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

Sidebar

Related Questions

I have parent/child relationship set up via Node Reference. A Child record can have
I know I can have 1 parent element and 1 child element in my
I have a simple parent-child situation where the parent can have multiple children. The
Using parent child relationship where a parent can have children while each child has
If I have a parent table and a child table where there can be
I have a simple parent/child tables. Contract is the parent table. Each contract can
How can I do a multi-level parent-child sort using Linq if I have a
Summary A parent can have many children. How do you write a service such
With the XmlSerializer I can have my members in different namespaces to the parent
i know i can have iframe in a html page, say parent.htm, and i

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.