of OOP and C++

Started by
15 comments, last by CTar 17 years, 10 months ago
Quote:Original post by Anonymous Poster
man->addObject(*obj);
cout addObject(*obj2);

you are still passing by value, pass by reference instead.

man->addObject(obj);
cout addObject(obj2);


Your (AP's) code is passing by pointer, the syntax isn't changed at all for reference, except that the type is different (you add &). And as the AP said, it seems addObject still takes its parameter by value. Also you should delete everything which is created with new, anything else will result in a memory leak. So you should also delete man.
Advertisement
Quote:Original post by CTar
Quote:Original post by Anonymous Poster
man->addObject(*obj);
cout addObject(*obj2);

you are still passing by value, pass by reference instead.

man->addObject(obj);
cout addObject(obj2);


Your (AP's) code is passing by pointer, the syntax isn't changed at all for reference, except that the type is different (you add &). And as the AP said, it seems addObject still takes its parameter by value. Also you should delete everything which is created with new, anything else will result in a memory leak. So you should also delete man.


"Your (AP's) code is passing by pointer..." this is passing by reference(not a c++ reference)

Quote:Original post by CTar
Quote:Original post by Anonymous Poster
man->addObject(*obj);
cout addObject(*obj2);

you are still passing by value, pass by reference instead.

man->addObject(obj);
cout addObject(obj2);


Your (AP's) code is passing by pointer, the syntax isn't changed at all for reference, except that the type is different (you add &). And as the AP said, it seems addObject still takes its parameter by value. Also you should delete everything which is created with new, anything else will result in a memory leak. So you should also delete man.


"Your (AP's) code is passing by pointer..." this is passing by reference(not a c++ reference)

Unintentionally double-posted. Other thread closed.
Quote:Original post by CTar
Quote:Original post by by
Thank you all, sometimes -like now- i love gdnet much more :)

Unfortunately, my questions not ended :)

Not about actually main topic but i dont know: has c/c++ got garbage collector?
for(int r = 0; r<5000; r++)
{
double r1;
}

I don't know what the loop has to do with anything... C and C++ doesn't have any garbage collector, all objects on the stack are destroyed automatically though. Imagine the following function:
void func(){  int * o1 = new o1;  int * o2 = o1;  int o3;  int o4 = o3;  int * o5 = &o3}

This code have one problem, o1. o1 is allocated on the heap. o2 is just a pointer. o3 and o4 is allocated on the stack and therefore automatically destroyed. o5 is also a pointer. So o5 and o2 doesn't need to be destroyed either because they just points to some memory. o1 needs to be destroyed though, because it have asked for heap-memory and need to tell that it's done. Therefore the function should look like this:
void func(){  int * o1 = new o1;  int * o2 = o1;  int o3;  int o4 = o3;  int * o5 = &o3  delete o1;}



---

Quote:what must i do for vector to don't copy objects, just point them ?


You need to use a vector of pointers, so CObjectManager should look like this:
*** Source Snippet Removed ***

All changes have been marked with a comment.



oh, first time i coldn't see the code completely. :)

output:
CObjectManager created!
CObject created! (17)
CObject created! (21)
0
CObject added! (17)
1
CObject added! (21)
2
Object0's value is: 17
Object1's value is: 21
3
CObject deleted! (17)
CObject deleted! (21)
4
Press any key to continue . . .

i think its ok now :)
Quote:Original post by Anonymous Poster
this is passing by reference(not a c++ reference)


What are you taking about, AFAIK this thread is about C++ (that's why it says C++ in the title) and C++ only have one kind of reference.

If you in C++ have the following:
T* o;f(o);

Then you pass the pointer to T by value or reference, but it will always be the pointer to T, never a reference to T.
Quote:Original post by dmail
There are two "references" in c++. The C++ reference which refers to the object/type and passing by reference which orignates from C.


"What are you taking about..." is it clear now?


I'm still not entirely sure what you mean, do you consider passing a pointer, passing by reference?

This topic is closed to new replies.

Advertisement