View Single Post
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#12
Originally Posted by saxen View Post
ok with 2) i've solve problem with >> operator...but anything else is working xD

i'm sure is a reference/pointer stuff. i mean, i'm adding to my QList<Service> a reference Service service in a method called "addItems". Will it be this reference deleted once the addItems is over even if it's now linked in my list?!

so should i use Service * service = new Service() and then add with list->append(*service) ?!
No. Don't get confused with this.
When you append an item to a collection in such a way, or assign it to a variable, it gets copied. (Its copy constructor or operator= gets called.)

Eg.
Code:
MyClass item; //creating a new item
item.stuff = 42;
MyClass item2 = item; //behind the scenes, item2 copies item
item.stuff = 8; //this line doesn't change item2
Note that by default, the MyClass type can't be copied.
You need to tell HOW it is gonna be copied.
That's why there is a copy constructor or an operator=.

Basically,
Code:
item2 = item;
is the same as
Code:
item2.operator=(item);
and only works if there is a
Code:
MyClass::operator=(const MyClass& other) { ... }
method in your class.

And
Code:
MyClass item2 = item;
is the same as
Code:
MyClass item2(item);
and only works if there is a
Code:
MyClass::MyClass(const MyClass& other) { ... }
constructor in your code.

Last edited by Venemo; 2010-06-03 at 23:06.
 

The Following User Says Thank You to Venemo For This Useful Post: