0

According to http://www.cplusplus.com/reference/stl/deque/push_front/ x is "Value to be copied to the new element. T is the first template parameter (the type of the elements stored in the container)."

Easy enough for built-in types, but now I'm creating a class, and a deque of objects of this class - what do I need for push_front to work?

I think I need constructor, but what function header?

milleniumbug
  • 15,379
  • 3
  • 47
  • 71
  • 7
    Pick one good introductory book from there : http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – FailedDev Dec 04 '11 at 19:23
  • 4
    Your class needs a `public` copy constructor. By default the compiler will generate one as needed, but if your class does things like allocating memory on the heap, you will need to write a custom copy constructor or you will get shallow copies. Since that enters the [Rule of Three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) territory, you need to also provide the destructor and assignment operator if you make your own copy constructor. – wkl Dec 04 '11 at 19:25

4 Answers4

4

AFAIK, only requirements on the type of element stored in a std::deque<> are:

  1. a public default constructor (use in members such as .resize())
  2. a public copy constructor (the container stores copies of items and may internally copy them at will)
  3. a public assignment operator (the container may internally overwrite items at will)
  4. a public nothrow() destructor (without which the container cannot guarantee its exception guarantees.

For example, this simple class works fine:

 class Person
 {
     std::string myName;
 public:
      void name(const string&);
      const std::string& name(const string&) const;
 };

because the compiler-generated defaults are good enough. It can be used like:

 std::dequeue<Person> people;
 people.push_back(Person());
 people.back().name("George");
André Caron
  • 44,541
  • 12
  • 67
  • 125
1

You need to have an assignment operator for your class, a destructor, and a copy constructor.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks, I knew I forgot something. – milleniumbug Dec 04 '11 at 19:34
  • Just remember the rule of three: If you need *one* of copy constructor, copy assignment and destructor, you need *all* of them. Note however that in C++11, you can provide move instead of copy, that is, if you have move construction/move assignment, you don't need to provide copy construction/copy assignment for your class (however then you should explicitly disable them). – celtschk Dec 04 '11 at 19:40
  • This answer is confusing. You don't need to define those. Those are defined for you and you only need to do it yourself when your class requires behavior different from the default. In addition `push_back` requires the argument to be a model of `CopyInsertable` (see Table 101 in 23.3.2). – pmr Dec 04 '11 at 19:42
  • @pmr I understand that the answer is not complete, but it does cover the majority of real-life situations. The worst thing that's going to happen is that you'll write slightly more code than you need to write. As far as the formal coverage goes, it's hard to impossible to rival Stroustrup's books, so I don't even try. – Sergey Kalinichenko Dec 04 '11 at 19:58
  • @dasblinkenlight: IMHO, that doesn't cover the *majority* of real life situations. It covers all objects that perform some internal resource allocation that needs to be managed explicitly. Is the many situations, all data members proper copy semantics and replacing the compiler-generated for custom copy semantics is not necessary. – André Caron Dec 04 '11 at 20:21
  • 1
    @dasblinkenlight: You could have had a more complete answer by simply changing the word 'define' to 'have'. That's 2 fewer letters. – Benjamin Lindley Dec 04 '11 at 20:40
  • @BenjaminLindley This is an excellent suggestion. I edited the answer, thank you very much! – Sergey Kalinichenko Dec 04 '11 at 20:49
0

Your class needs to be copyable (copy-constructor and assignment operator) and have a public destructor; a default constructor also helps.

Note that if your class isn't complex, the compiler-provided copy constructor, assignment operator, and destructor may suffice.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
0

If you push in the deque instance if you own class. The copy constructor has been called. You must provide it or use default

Yappie
  • 399
  • 2
  • 8