what's the difference between &conn and *conn
Hi everyone,
I was trying to add a class object pointer to a QList<A*> list.
Code:
Class A
{
{
return myName;
}
}
Class B
{
QList<A*> list;
A myOb;
list.append(&myOb);
}
{
for(int i=0;i<list.size();++i){
if (list[i].Desc() ==s) {
}
}
}
}
I get segmentation fault I think.
Code:
Class A
{
{
return myName;
}
}
Class B
{
QList<A*> list;
A *myOb=new A();
list.append(myOb);
}
{
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.
Re: what's the difference between &conn and *conn
Quote:
Originally Posted by
yyiu002
Code:
Class A
{
{
return myName;
}
}
Class B
{
QList<A*> list;
A myOb;
list.append(&myOb);
}
{
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.
Re: what's the difference between &conn and *conn
I still didn't get it? They both are pointers, but why just first usage of poiner was destroyed?
Re: what's the difference between &conn and *conn
Code:
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)
Code:
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
Re: what's the difference between &conn and *conn
Many thanks to all of you.