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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:24:33+00:00 2026-05-14T04:24:33+00:00

In the following code: class A { }; class B : public A {

  • 0

In the following code:

class A {
};
class B : public A {
};
class C : public A {
   int x;
};

int main (int argc, char** argv) {
   A* b = new B();
   A* c = new C();

   //in both cases, only ~A() is called, not ~B() or ~C()
   delete b; //is this ok?
   delete c; //does this line leak memory?

   return 0;
}

When calling delete on a class with a non-virtual destructor with member functions (like class C), can the memory allocator tell what the proper size of the object is? If not, is memory leaked?

Secondly, if the class has no member functions, and no explicit destructor behaviour (like class B), is everything ok?

I ask this because I wanted to create a class to extend std::string, (which I know is not recommended, but for the sake of the discussion just bear with it), and overload the +=, + operator. -Weffc++ gives me a warning because std::string has a non virtual destructor, but does it matter if the sub-class has no members and does not need to do anything in its destructor?

FYI the += overload was to do proper file path formatting, so the path class could be used like:

class path : public std::string {
    //... overload, +=, +
    //... add last_path_component, remove_path_component, ext, etc...
};

path foo = "/some/file/path";
foo = foo + "filename.txt";
std::string s = foo; //easy assignment to std::string
some_function_taking_std_string (foo); //easy implicit conversion
//and so on...

I just wanted to make sure someone doing this:

path* foo = new path();
std::string* bar = foo;
delete bar;

would not cause any problems with memory allocation?

  • 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-14T04:24:33+00:00Added an answer on May 14, 2026 at 4:24 am

    So everyone has been saying you cant do it – it leads to undefined behaviour. However
    there are some cases where it is safe. If you are never creating instances of your class dynamically then you should be OK. (i.e. no new calls)

    That said, it is generally considered a bad thing to do as someone might try to create one polymorphically at some later date. ( You may be able to protect against this by having a private unimplemented operator new, but I’m not sure. )

    I have two examples where I don’t hate deriving from classes with non-virtual destructors.
    The first is creating syntactic sugar using temporaries … here’s a contrived example.

    class MyList : public std::vector<int>
    {
       public:
         MyList operator<<(int i) const
         {
           MyList retval(*this);
           retval.push_back(i);
           return retval;
         }
       private: 
         // Prevent heap allocation
         void * operator new   (size_t);
         void * operator new[] (size_t);
         void   operator delete   (void *);
         void   operator delete[] (void*);
    };
    
    void do_somthing_with_a_vec( std::vector<int> v );
    void do_somthing_with_a_const_vec_ref( const std::vector<int> &v );
    
    int main()
    {
       // I think this slices correctly .. 
       // if it doesn't compile you might need to add a 
       // conversion operator to MyList
       std::vector<int> v = MyList()<<1<<2<<3<<4;
    
      // This will slice to a vector correctly.
       do_something_with_a_vec( MyList()<<1<<2<<3<<4 );
    
      // This will pass a const ref - which will be OK too.
       do_something_with_a_const_vec_ref( MyList()<<1<<2<<3<<4 );
    
      //This will not compile as MyList::operator new is private
      MyList * ptr = new MyList();
    }
    

    The other valid usage I can think of comes from the lack of template typedefs in C++. Here’s how you might use it.

    // Assume this is in code we cant control
    template<typename T1, typename T2 >
    class ComplicatedClass
    {
      ...
    };
    
    // Now in our code we want TrivialClass = ComplicatedClass<int,int>
    // Normal typedef is OK
    typedef ComplicatedClass<int,int> TrivialClass;
    
    // Next we want to be able to do SimpleClass<T> = ComplicatedClass<T,T> 
    // But this doesn't compile
    template<typename T>
    typedef CompilicatedClass<T,T> SimpleClass;
    
    // So instead we can do this - 
    // so long as it is not used polymorphically if 
    // ComplicatedClass doesn't have a virtual destructor we are OK.
    template<typename T>
    class SimpleClass : public ComplicatedClass<T,T>
    {
      // Need to add the constructors we want here :(
      // ...
       private: 
         // Prevent heap allocation
         void * operator new   (size_t);
         void * operator new[] (size_t);
         void   operator delete   (void *);
         void   operator delete[] (void*);
    }
    

    Heres a more concrete example of this. You want to use std::map with a custom allocator for many different types, but you dont want the unmaintainable

    std::map<K,V, std::less<K>, MyAlloc<K,V> >
    

    littered through your code.

    template<typename K, typename V>
    class CustomAllocMap : public std::map< K,V, std::less<K>, MyAlloc<K,V> >
    {
      ...
       private: 
         // Prevent heap allocation
         void * operator new   (size_t);
         void * operator new[] (size_t);
         void   operator delete   (void *);
         void   operator delete[] (void*);
    }; 
    
    MyCustomAllocMap<K,V> map;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 450k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check out the MySQL Regex methods. Something like the following… May 15, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer It only works if you wrap your whole page inside… May 15, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer The 'thing' list is excepting a Guid value. You should… May 15, 2026 at 8:41 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.