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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:57:46+00:00 2026-06-18T14:57:46+00:00

I was unsure about whether STL containers are entirely copied when passed. First, it

  • 0

I was unsure about whether STL containers are entirely copied when passed. First, it worked (so the “fluttershy” element didn’t get added, that was good). Then I wanted to track the construction and destruction of entries….

#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
using namespace std;

int nextid = 0;

class Entry {
public:
    string data;
    int myid;
    Entry(string in) {
        data = in;
        myid = nextid;
        nextid++;
        printf("Entry%02d\n", myid);
    }
    ~Entry() { printf("~Entry%02d\n", myid); }
};

class Meep {
public:
    vector<Entry> stuff;
};

void think(Meep m) {
    m.stuff.push_back(Entry(string("fluttershy")));
}

int main() {

    Meep a;
    a.stuff.push_back(Entry(string("applejack")));
    think(a);
    vector<Entry>::iterator it;
    int i = 0;
    for (it=a.stuff.begin(); it!=a.stuff.end(); it++) {
        printf("a.stuff[%d] = %s\n", i, (*it).data.c_str());
        i++;
    }

    return 0;
}

Produces the following unexpected output( http://ideone.com/FK2Pbp ):

Entry00
~Entry00
Entry01
~Entry00
~Entry01
~Entry00
~Entry01
a.stuff[0] = applejack
~Entry00

That a has only one element is expected, that is not the question. What seriously confuses me is how can one entry be destructed several times ?

  • 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-18T14:57:48+00:00Added an answer on June 18, 2026 at 2:57 pm

    What you’re seeing is the destruction of temporary instances.

    a.stuff.push_back(Entry(string("applejack")));
    

    This line creates a temporary instance which is then copied to another new instance in the container. Then the temporary is destroyed. The instance in the container is destroyed when the entry is removed or the container is destroyed.

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

Sidebar

Related Questions

I'm making my first steps with unit testing and am unsure about two paradigms
I'm unsure about whether to use a mutable struct or a mutable class. My
Unsure about static variables. import java.io.File; public class Logger { public static final File
I am unsure about which monitoring framework to use. Currently I am looking at
So I'm kind of unsure about something. What I want is one class to
All considerations about when to use which aside, I am still unsure about pointer
I'm new to SQL and I'm a little unsure about the best way to
I use sl4j / logback as a logging framework. I am unsure about the
I am building a small chat application for friends, but unsure about how to
I'm about to create a java crossword application but I am unsure of what

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.