PDA

View Full Version : what's the difference between &conn and *conn



yyiu002
22nd June 2010, 05:36
Hi everyone,
I was trying to add a class object pointer to a QList<A*> list.



Class A
{
QString myName;
public QString Desc()
{
return myName;
}

}

Class B
{
QList<A*> list;
public void Add(QString s){
A myOb;
list.append(&myOb);
}
public void search(QString s)
{
for(int i=0;i<list.size();++i){
if (list[i].Desc() ==s) {

}
}
}
}


I get segmentation fault I think.




Class A
{
QString myName;
public QString Desc()
{
return myName;
}

}

Class B
{
QList<A*> list;
public void Add(QString s){
A *myOb=new A();
list.append(myOb);
}
public void search(QString s)
{
for(int i=0;i<list.size();++i){
if (list[i].Desc() ==s) {

}
}
}
}


It works fine.

What's the differnce between list.append(&myOb) and list.append(myOb) in above code.

tbscope
22nd June 2010, 06:48
Class A
{
QString myName;
public QString Desc()
{
return myName;
}

}

Class B
{
QList<A*> list;
public void Add(QString s){
A myOb;
list.append(&myOb);
}
public void search(QString s)
{
for(int i=0;i<list.size();++i){
if (list[i].Desc() ==s) {

}
}
}
}


I get segmentation fault I think.

A couple of things you should take into consideration:
1. You do not have a constructor
2. Everything is private by default
3. Try to put the implementation into a cpp file

Programmatically, there's no difference between using &myObj in your first example and myObj in your second example. Both point to an address.
However, there's a world of difference when it comes to where the address is defined and how long it lives.

In short, in the code above you get a segmentation fault because myObj doesn't exist anymore when you want to access it in your search function.
Your object myObj is a local object defined inside the scope of the add function. It stops existing after you leave the add function, even if you have added to the list.
Thus, there's nothing wrong with you adding the object to the list. It only doesn't exist anymore a fraction of a second later.

The second example works because the pointer isn't destroyed when you leave the scope of the add function. However, if you didn't use the list to append the object, it would have really gone out of scope, as in you don't have any control anymore on the object. But in your example you've added it to a list that is available in class B, so that's no problem.

yyiu002
22nd June 2010, 22:44
I still didn't get it? They both are pointers, but why just first usage of poiner was destroyed?

Zlatomir
22nd June 2010, 22:53
public: void Add(QString s){
A myOb; // here the myObj will "live" only in this function
list.append(&myOb);
} //here that object is deleted
// here you don't have it any more
// the myObj scope is in that function,
// and you will try to access it later with the pointer from the list (but the object is gone)



public: void Add(QString s){
A *myOb=new A(); //here you allocate memory on the heap, this will "live" until delete is called
list.append(myOb);
}
// the object is still valid after the execution of the function
// only the pointer "life" is ended, but you have the pointer from the list that will take care of the actual object


References are not pointers. DO NOT code like they can be changed, they are two different types of indirect access to an object

yyiu002
23rd June 2010, 01:43
Many thanks to all of you.