1

if i have a class "Movie" as follows:(c++)

class Movie{
private:
    string name; 
    string year; 
    list<Employee ?> e;
}

what is the difference between :

list<Employee *> e  
list<Employee &> e  

and how do i implement the destructor ~Movie() for each variation ?

Mahmod
  • 23
  • 4

3 Answers3

2

You cannot have a list of references list<Employee &> e because the elements in the list have to be Assignable and a reference cannot be assigned to.

russoue
  • 5,180
  • 5
  • 27
  • 29
1

You can't have an STL container of references to objects. That is,

std::list<Employee&> e;

doesn't have a chance to compile. This is attributed to the fact that STL containers require that their elements are Erasable [Note: Before C++11 the requirements were to be CopyAssignable and CopyConstructible]. Unfortunately, references are not.

Also since you have the C++11 tag, you should be aware that use of raw pointers is highly discouraged in favour of smart pointers like std::unique_ptr and std::shared_ptr. Thus, is better to avoid this:

std::list<Employee*> e;

in favour of something like:

std::list<std::shared_ptr<Employee>> e;
101010
  • 41,839
  • 11
  • 94
  • 168
0

As stated in the comments list<Employee &> e won't compile. If you want to do a reference to the class and not a pointer, you can use list<std::reference_wrapper<Employee>> e. The type has to be Assignable.

mculp
  • 59
  • 1
  • 7