Skip to main content
gamedev.net
🔒 Locked

Class Pointers ?

Started by Endemoniada Mar 30, 2004 at 9:59 PM 6 replies 550+ views
Original Post
Endemoniada
Endemoniada
Hi guys, is this legal: class CMyClass { ...some stuff } *LPMYCLASS; Then I can just go LPMYCLASS for a pointer declaration.
Fruny
Fruny
It is legal but it declares a pointer instance (and the class itself), not a pointer type.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Qw3r7yU10p!
Qw3r7yU10p!
You need to make a typedef


{
typedef CMyClass* CMyClassPtr;

CMyClassPtr p(new CMyClass());
delete p;
}


you could replace it with a smart pointer and then you wouldn't need to worry about memory management.


{
typedef shared_ptr<CMyClass> CMyClassPtr;

CMyClassPtr p(new CMyClass());
}//end of scope shared_ptr will clean up memory for you



[edited by - petewood on March 31, 2004 3:01:03 AM]
Endemoniada
Endemoniada
Thanks guys.

What''s the best way to pass a pointer to a class to a function ?

Like this:

// prototype
void MyFunction(CMyClass* pMyClass);

// caller
CMyClass myClass;
MyFunction(&myClass);

Or by reference like this:

// prototype
void MyFunction(CMyClass& myClass);

CMyClass myClass;
MyFunction(myClass);

Isn''t it the same thing ? I really never understood passng by reference as opposed to passing a pointer. Thanks again.
Qw3r7yU10p!
Qw3r7yU10p!
A reference refers to an actual object, it always does, you can't have a null reference and you can't change the value of a reference once it is created. A pointer can be made to point to whatever you want, it may be null or uninitialised and can be changed.

I would say it is preferable to pass things by reference rather than by pointer, unless you want to be able to make decisions based upon a pointer being null.

Passing by pointer and reference are going to be the same efficiency. People are more used to using pointers in C++ because there wasn't a reference type in C. C APIs use pointers then but in C++ code I'd go with references.

You could get a situation where you might have what you'd nominally call a null reference. But actually to get it you'd have to dereference a null pointer which would either be due to incompetence or maliciousness.

int* p = 0;
int& r = *p;

Some alternatives to using null pointers to indicate some important condition are you could overload functions, pass enumerations instead, or possibly make use of the null object idiom.

[edited by - petewood on March 31, 2004 9:00:16 AM]
Shannon Barber
Shannon Barber
Non-constant references are write-only code.

If you don''t change it, pass by const ref; if you change it, pass by pointer.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Willm
Willm
quote:
Original post by Magmai Kai Holmlor
Non-constant references are write-only code.

If you don''t change it, pass by const ref; if you change it, pass by pointer.


Whats wrong with passing a non-const reference, if you plan to change it?
Agony
Agony
quote:
Original post by Willm
Whats wrong with passing a non-const reference, if you plan to change it?

When you pass a parameter using a pointer, then it is obvious to the caller of the function, because they have to either use the address-of operator, or already have a pointer. They can''t get away with just passing in the object itself. Because of this, the caller is always aware that the object might change.

However, if you have a non-const reference, the caller might not realize that they''re passing in a reference, since it looks the same as passing in a copy. Thus, they may think that their object won''t change, when in fact it does.

I don''t know if I have a problem with non-const references, but some people do, and they do have some good reasons to.


int Agony() { return *((int*)0); }    Mwahaha... >8)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.