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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:43:05+00:00 2026-06-04T14:43:05+00:00

On a project, I have the following problem : I have a very simple

  • 0

On a project, I have the following problem :

I have a very simple inheritance scheme (I need inheritance and not composition) :

class Base

-> class DerivedA

-> class DerivedB

-> class DerivedC

A, B and C derive from Base and that’s all.
So now I have 2 choices :

public inheritance with virtuality

private inheritance without virtuality

For some optimization reasons (I need a lot of inlining) I don’t want virtuality … and I don’t want private inheritance. I think that the only option that remains is CRTP. But the base class have like 300 functions and implementing CRTP in it will be a real pain.

So I wonder if the following solution is valid : I use CRTP only in the destructor of the base class :

template<class TCRTP> class Base
{
    ~Base() {delete static_cast<TCRTP*>(this);}
}

where TCRTP will be DerivedA, B or C and I do public inheritance.
Is it perfectly ok, or problematic ?

Thank you very much.

  • 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-04T14:43:07+00:00Added an answer on June 4, 2026 at 2:43 pm

    Your destructor is definitely wrong. The destructor of a class does not and must not delete the memory for the object.

    What’s your objection to public inheritance without virtual functions? There are (at least) a couple of ways to prevent someone accidentally deleting a derived object through a base pointer. One is to make the base destructor protected.

    Another is to stuff dynamically-allocated instances of the derived class straight into a shared_ptr. This can even be a shared_ptr<Base>:

    std::shared_ptr<Base> foo(new DerivedA(...));
    

    Because shared_ptr has a template constructor that captures the type of its argument, the Base* pointer will be converted to DerivedA* in the deleter function associated with the shared_ptr, and hence deleted correctly. Nobody should ever be so daft as to try to extract a pointer out of a shared_ptr and delete it as Base*.

    Of course if you have no virtual functions, then the trick is only really useful when the only difference between the derived classes is what they set up in their constructors. Otherwise you’ll end up needing to downcast the Base* pointer from the shared_ptr, in which case you should have used a shared_ptr<DerivedA> to begin with.

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

Sidebar

Related Questions

I have the following problem. I need to organize automatic upload to deploy server
This problem is probably very simple to solve but it is not clear to
I have a very simple class that has a single property and which inherits
I have the following code in a project I am doing for class. I
I have a very simple makefile, that basically does the following: # Pre-compiled header
In my project I have the following three interfaces, which are implemented by classes
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
I am working on a project and have the following psuedocode: // create a
I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;
I have notices following: if I have a project with minimum files in res

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.