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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:44:48+00:00 2026-06-04T02:44:48+00:00

Consider this piece of code: #include <vector> #include <iostream> using namespace std; class Base

  • 0

Consider this piece of code:

#include <vector>
#include <iostream>
using namespace std;

class Base
{
    char _type;
public:
    Base(char type):
        _type(type)
    {}

    ~Base() {
        cout << "Base destructor: " << _type << endl;
    }
};

class uncopyable
{
    protected:
        uncopyable() {}
        ~uncopyable() {}
    private:
        uncopyable( const uncopyable& );
        const uncopyable& operator=( const uncopyable& );
};

class Child : public Base, private uncopyable
{
    int j;
public:
    Child():
        Base('c')
    {}
    ~Child() {
        cout << "Child destructor" << endl;
    }
};


int main()
{
    vector<Base> v;
    Base b('b');
    Child c;

    v.push_back(b);
    v.push_back(c);
    return 0;
}

The output on my system is:

Base destructor: b
Child destructor
Base destructor: c
Base destructor: b
Base destructor: b
Base destructor: c

My questions are:

  • Why is the destructor of Base (with type b) called three times instead of two (do we have more than two copies of object b)?

  • What happens when we copy an object of type Child, considering the copy-constructor of one of its parents is private. Is it undefined behavior?

  • I was expecting to get a compile-time error whenever I try to copy an object of type Child. I thought the child’s default copy-constructor would try to call the private copy-constructor of the Uncopyable class and cause compilation error. Why doesn’t it give compile errors?

The reason the code is designed this way is because the Child class is huge.

The desired behavior is throwing away the child data whenever a client tries to copy a Child object (calling the destructor of Child without calling the destructor of Base).

This piece of code achieves that, but I guess it results in undefined behavior and has memory leak (never calls the destructor of Child for the copied instance).

  • 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-04T02:44:50+00:00Added an answer on June 4, 2026 at 2:44 am

    Here is what happens in your code:

    int main() 
    { 
        vector<Base> v;    // 1
        Base b('b');       // 2
        Child c;           // 3
    
        v.push_back(b);    // 4
        v.push_back(c);    // 5
        return 0; 
    }                      // 6
    
    1. line 1: vector v constructed

    2. line 2: Base b constructed (calling Base’s constructor)

    3. line 3: Child c constructed (calling Child’s constructor and Base’s constructor)

    4. line 4: v is current at maximum capacity and needs to be resized.
      Memory is allocated for 1 element of Base by v.
      Base b copied into v[0] (calling Base’s copy constructor).

    5. line 5: v is again at maximum capacity and needs to be resized.
      Memory is allocated for 2 elements of Base by v.
      The old v[0] is copied into the new v[0] (calling Base’s copy constructor).
      The old v[0] is deleted (calling Base’s destructor (“Base destructor: b”)).
      Child c is copied into v[1] (calling Base’s copy constructor).

    6. line 6: c, b, and v run out of scope.
      Child c is deleted (calling Child’s destructor (“Child destructor”) then Base’s destructor (“Base destructor: c”).
      Base b is deleted (calling Base’s destructor (“Base destructor: b”)).
      Base v[0], v[1] are deleted (calling Base’s destructor twice (“Base destructor: b”, “Base destructor: c”)).

    There is no memory leak – for every constructor in the above sequence a corresponding destructor is called.

    Additionally, you seem to be very confused about copy constructors. Child c gets passed to push_back as a Base& – which then calls Base’s copy constructor as expected. Since Base’s implicit copy constructor is not virtual or overriden, having Child derive from uncopyable does nothing to change this.

    Note that a vector<Base> cannot ever store an object of type Child; it only knows to allocate enough memory for a Base. What occurs when assigning an instance of Child to a Base is known as slicing, which, while often unintended and misunderstood, seems like it may actually be what you want in your described scenario.

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

Sidebar

Related Questions

Consider this piece of code: public abstract class Validator { protected Validator() { }
Consider this piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
Consider this simplest piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text;
Let's consider this piece of code as our example: import java.awt.*; class Maze extends
Consider this piece of code: class A { void methodX() { // snip (1
Consider this piece of code: struct Trade { float Price; char* time; int shares;
Consider this small piece of code from apscheduler.scheduler import Scheduler import time class First():
consider this piece of scala swing code detail.reactions += { case ButtonClicked(but) => detail.contents
Consider this one piece of Perl code, $array[$x]->{foo}->[0]= January; I analyze this code as
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {

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.