Advertisement

Question on passing pointers to functions

Started by August 02, 2002 10:15 AM
10 comments, last by Radagar 22 years, 3 months ago
quote: Original post by Anonymous Poster
It''s not neccesary to create a pointer for the sole purpose of passing it to a function. This is where the reference operator comes in.

The reference operator is not required here. It''s never required. It does EXACTLY the same things a pointer can do, with some limitations. In this instance, either would work fine.

quote:
This is pure abuse of the new operator. Sure it''s legal, but think of the moral implications: memory leaks.

Errr.... moral implications? hehe...kay. Anyways, this is not only legal, but good programming practice, as it exactly specifies object lifetimes. True, there''s the potential for memory leaks, but that''s the price you pay for flexibility. It''s silly to dismiss a technique just because use of it without understanding its ramifications can lead to problems.

quote:
Another example where the reference operator is clearly superior considering a fixed number of Monsters and players allocated.

Perhaps you could expand on this? what advantage do references give you here?

*** 500 ERROR ***


Don''t listen to me. I''ve had too much coffee.
quote: Original post by Sneftel
Errr.... moral implications? hehe...kay.
Yes sir. One could write an application with memory leaks and not technically be required to fix it. The program will run fine, the leaks don''t occur until after the program ends and the objects are left on the heap (actually, memory leaks occur when you lose the pointer to a block allocated on the heap and are unable to deallocate it). This is a devious practice of sloth, but I have seen it done MANY of times. As for lifetime, static objects live from when they are created until the program ends. I personally hate globals (they are static too) because they aren''t Object Orientated enough for me. However, I love Singletons which are also static.
quote: Original post by James
Another example where the reference operator is clearly superior considering a fixed number of Monsters and players allocated.
Original post by Sneftel
Perhaps you could expand on this? What advantage do references give you here? Certainly. In this particular senario, the programmer doesn''t have to call a delete if the object is created on the stack. When the number of objects is known at design time, all one has to do is create it on the stack and use a reference to it. If you are looking for a long lifetime, then create the object as static or put it in global space.

On the other hand, if the number of Monsters or players wasn''t known at design time, then I would suggest STL. As a last resort will I ever suggest using new or delete. That''s personal preference I know, but this is the way I see it: new and delete are headaches if you don''t know how to use them well. I know it''s terrible to assume that whoever I''m suggesting a remedy for isn''t good with new and delete, but it''s better safe than sorry.

In closing: I think you nailed it when you suggested that he make the functions members of the class. Oh, and Thanks for the tip on UML class diagrams. I''ve only briefly looked over it so far, but I believe it will help me to think Object Orientedly.

-James

This topic is closed to new replies.

Advertisement