PDA

View Full Version : Object or pointer to object?



arcull
21st July 2014, 13:30
Hi there. To make it clear, I'm no OOP, C++ or Qt expert, so I made a few simple functions to clear up a few things about objects and pointer to objects. Here is the code, a class named cat:
class Cat {
public:
void SetColor(string clr) {
color = clr;
}

string GetColor() {
return color;
}

private:
string color;
};, and sample usage:
void MainWindow::GenerateCats() {
Cat *tofi = new Cat();
tofi->SetColor("orange");
printf("tofi color1 :%s\n",tofi->GetColor().c_str());

Cat smuki;
printf("smuki color 1:%s\n",smuki.GetColor().c_str());
smuki.SetColor("black");
printf("smuki color 2:%s\n",smuki.GetColor().c_str());

Cat *sivkolin = &smuki;
printf("sivkolin color 1:%s\n", sivkolin->GetColor().c_str());

smuki.SetColor("gray");
printf("sivkolin color 2:%s\n", sivkolin->GetColor().c_str());

ProcessCats1("tofi",*tofi,"white");
printf("tofi color 2 :%s\n", tofi->GetColor().c_str());

ProcessCats1("smuki",smuki,"white");
printf("smuki color 3:%s\n", smuki.GetColor().c_str());

ProcessCats2("smuki",&smuki,"no color");
printf("smuki color 4:%s\n", smuki.GetColor().c_str());

printf("sivkolin color 3:%s\n", sivkolin->GetColor().c_str());

ProcessCats2("sivkolin",sivkolin,"gold");
printf("sivkolin color 4:%s\n", sivkolin->GetColor().c_str());

printf("smuki color 5:%s\n", smuki.GetColor().c_str());

delete tofi;
//delete smuki;
//smuki = NULL;
delete sivkolin;
}

void MainWindow::ProcessCats1(string name,Cat CatSample,string clr) {
printf("ProcessCat1 Cat %s color:%s\n",name.c_str(),CatSample.GetColor().c_str());
CatSample.SetColor(clr);
}

void MainWindow::ProcessCats2(string name,Cat *CatSample,string clr) {
printf("ProcessCats2 Cat %s color:%s\n",name.c_str(), CatSample->GetColor().c_str());
CatSample->SetColor(clr);
} the output looks like:
tofi color1 :orange
smuki color 1:
smuki color 2:black
sivkolin color 1:black
sivkolin color 2:gray
ProcessCat1 Cat tofi color:orange
tofi color 2 :orange
ProcessCat1 Cat smuki color:gray
smuki color 3:gray
ProcessCats2 Cat smuki color:gray
smuki color 4:no color
sivkolin color 3:no color
ProcessCats2 Cat sivkolin color:no color
sivkolin color 4:gold
smuki color 5:gold My questions:
Is "->" notation when accessing members addressed with a pointer a C++ or Qt specific?
Function ProcessCats1 expects an object of type Cat not a pointer, why must I use
ProcessCats1("tofi",*tofi,"white"); instead of
ProcessCats1("tofi",&tofi,"white");
Same analogy with function ProcessCats2 which requires a pointer to object Cat and I do
ProcessCats2("smuki",&smuki,"no color");, but smuki is an object, why do I need the "&" meaning "the value at address".
I can delete object created on the heap like this
delete tofi;, but how do I delete smuki in the scope of same function if I need (I know it gets destroyed when out of scope).
When I do
delete sivkolin I delete just the pointer to value of smuki, so I can still access object smuki right?
Much thanks if advance for your help.

wysota
21st July 2014, 14:12
Is "->" notation when accessing members addressed with a pointer a C++ or Qt specific?
C/C++


Function ProcessCats1 expects an object of type Cat not a pointer, why must I use
ProcessCats1("tofi",*tofi,"white"); instead of
ProcessCats1("tofi",&tofi,"white");
Because * is a dereference operator that returns an object under a given pointer and operator & is a reference operator that returns a pointer to a given object.

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B



Same analogy with function ProcessCats2 which requires a pointer to object Cat and I do
ProcessCats2("smuki",&smuki,"no color");, but smuki is an object, why do I need the "&" meaning "the value at address".

See above.


I can delete object created on the heap like this
delete tofi;, but how do I delete smuki in the scope of same function if I need (I know it gets destroyed when out of scope).

It's a matter of understanding heap-allocated objects and stack-allocated objects rather than scopes. Every local variable is destroyed when it goes out of scope by unwinding the stack. Heap-based variables (allocated using new operator) are never destroyed automatically.


When I do
delete sivkolin I delete just the pointer to value of smuki
No, delete takes a pointer to the object that is to be deleted. So it is the object that gets deleted and not the pointer. The pointer will be freed when the scope of its declaration ends.


, so I can still access object smuki right?

No, but you can still access the pointer :)

arcull
21st July 2014, 14:39
thanks for explanation wysota, but I still don't know how to delete object created on the stack, which is destroyed as soon as the function gets out of scope, but let's I want to delete it inside the same function. Remember I have
Cat *tofi = new Cat();
Cat smuki;

delete tofi; //object on the heap deleted ok
delete smuki; // I can't do this
smuki = NULL; //neither this Thanks again.

stampede
21st July 2014, 14:41
It will be deleted automatically when it goes out of scope.

arcull
21st July 2014, 14:52
It will be deleted automatically when it goes out of scope. Thanks I know that, but can it be deleted while still in scope. It is not that I really need this, but would just like to make clear what can and what can't be done, thanks.

wysota
21st July 2014, 15:02
No, it cannot. The definition of a local variable is that its life is determined by the scope of its declaration. But you can limit the scope if you want.


Cat *tofi = new Cat();
{
Cat smuki;

delete tofi; //object on the heap deleted ok
} // smuki undeclared past this point

arcull
21st July 2014, 16:38
No, it cannot. The definition of a local variable is that its life is determined by the scope of its declaration. But you can limit the scope if you want.

Qt Code:
Switch view

Cat *tofi = new Cat();
{
Cat smuki;

delete tofi; //object on the heap deleted ok
} // smuki undeclared past this point Very good, thanks.