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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:02:10+00:00 2026-06-12T17:02:10+00:00

I have a Base class, that is considered read-only, and in it’s virtual desructor

  • 0

I have a Base class, that is considered read-only, and in it’s virtual desructor it does nothing.
Now I derive that Base class into a Derived class, which is writable, and in it’s destructor, it deletes the Base member:

class Base
{
    virtual ~Base() {}
    void* Data;
}
class Derive : public virtual Base
{
    virtual ~Derive() { delete Data; }
}

Ignoring the syntactically incorrect code above, if I were to pass a Derive instance into a Function that takes the Base class as a reference:

void Function(const Base& base)
{
   ...
}

...
Derive der = Derive();
...
Function(der);

Would the Derived destructor be called at the end of the Function scope? I had trouble looking for the right keywords to find an answer, so my apologies if it’s been asked before. I am assuming the C++ treats references for what typw they are and not for type they could be, but I could be wrong.

  • 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-12T17:02:11+00:00Added an answer on June 12, 2026 at 5:02 pm

    No it wouldn’t, because the object doesn’t go out of scope. It goes out of scope, and is automatically destroyed after the call to Function.

    {
      Derive der = Derive();
      //...
      Function(der);
      //...
      //der still alive here
    } //der is destroyed here, all destructors are called correctly
    

    If you were to pass the parameter by value instead of by reference:

    void Function(Base base)
    {
       ...
    }
    

    the object would be sliced. The copy created inside Function is an object of type Base (not Derive) so only the Base destructor would be called when the function exits.

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

Sidebar

Related Questions

In the project I'm working on now, we have base Entity class that looks
I have a Base class that needs a Generic type. That can be either
I have a base class ( car ) and a class that inherit the
Suppose I have a Base class and a Child class that inherits from Base
I have a Generic Base Class that I want to allow one of two
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
I have a class that inherits from a base class (this contains a lot
I have a class ClassA that is a base class of other classes. I
So I have a base class with a constructor that takes (T V) .

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.