1

I am trying to use the push_front() function on a list of a class I created. I have it in a for loop but whenever the loop inserts the new member into the list, it is automatically destroyed right after, I'm assuming because it is out of scope. My question is how do I add these items permanently.

Example:

class foo
{
public:
    foo(){std::cout << "creation" << std::endl;}
    ~foo(){std::cout << "destruction" << std::endl;}
    bool create() {return true;}
};

int main()
{
    std::list<foo> foolist(4);
    for (std::list<foo>::iterator i=foolist.begin(); i!=foolist.end(); i++)
    {
        foolist.push_front(foo());
    }
    return 0;
}

This is the output I'm getting:

creation
creation
creation
creation
creation
destruction
creation
destruction
creation
destruction
creation
destruction
destruction
destruction
destruction
destruction
destruction
destruction
destruction
destruction

When this is what I am trying to achieve:

creation
creation
creation
creation
creation
creation
creation
creation
destruction
destruction
destruction
destruction
destruction
destruction
destruction
destruction
9
  • Why does the title refer to an if statement when there is no if in the code? Moreover, why are you iterating over the list while simultaneously calling push_front on it? Commented Jul 17, 2014 at 22:23
  • 1
    You are adding them. The temporaries created in the intermediate add are what is being destroyed. Change that to foolist.emplace_front(); (no content in the parens). Note you will need a C++11 toolchain for this to work. Commented Jul 17, 2014 at 22:24
  • @jwodder, Sorry about that, this is just an example, not the actual thing I'm working on; in my project, I have an if statement in the for loop that determines creation, but I realized in making this example it doesn't change anything (that the for loop is the one actually responsible.) Commented Jul 17, 2014 at 22:27
  • @WhozCraig, I tried doing what you suggested, but I get the same output? How can I make sure I have the c++11 toolchain? Commented Jul 17, 2014 at 22:28
  • 1
    My crystal ball tells me your calling emplace_front() like this: emplace_front(foo(args...)). Don't. Call it like this: emplace_front(args...). If args... is nothing, then call it like this: emplace_front() (like I showed in my first comment). Regarding your IDE, make sure -std=c++11 is passed as a config switch during compile and link. Commented Jul 17, 2014 at 22:38

2 Answers 2

5

Let's walk through this program one line at a time.

std::list<foo> foolist(4);

Default constructor for foo called 4 times.

foolist.push_front(foo());

This statement runs 4 times. Each time, a foo is default constructed. push_front calls the implicitly defined move constructor in order to move this temporary foo object into the list. (Or the copy constructor if you're still in C++03 mode.) You don't see anything printed, because it's not the default constructor. At the end of each iteration, the temporary foo is destroyed.

    return 0;
}

The list now contains 8 foos, so the destructor is called 8 times.

Sign up to request clarification or add additional context in comments.

7 Comments

If this is a temporary foo being created and destroyed, how do I instead create an entirely new foo directly into the list? I am trying to create a program that outputs based on class member's creation and destruction ("Birth" and "Death" of bunnies.. haha)
@user3850982 Use foolist.emplace_front(), as suggested in the question comments.
Thank you Brian. My emplace_front() doesn't seem to work correctly, as I get the same output as when I use push_front(). What should I do with my IDE to get it to work properly?
@user3850982 Looks like your problem is solved in the question comments.
Yeah, both of these answers deserve upticks. They're both correct. there ya go!
|
2

The extra "creation" and "destruction" are due to the temp variables you're creating by calling

foo()

Those temp variables get copied into brand new items in the list which ALSO get created when push_front gets called and destroyed when the list falls out of scope

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.