simple c++ question
-
what's the difference between: int func(classA & test) and int funct(classA * test) with differences i don't mean, the first is by reference and the second one is a pointer. But what i want to know is, what are the advantages / disadvantages of those 2 ways of working to each other? And when should i use the first way, and when the second way? why am i asking this? because sometimes i'm programming i don't know for which one to chose, because they both work for me. thx
-
what's the difference between: int func(classA & test) and int funct(classA * test) with differences i don't mean, the first is by reference and the second one is a pointer. But what i want to know is, what are the advantages / disadvantages of those 2 ways of working to each other? And when should i use the first way, and when the second way? why am i asking this? because sometimes i'm programming i don't know for which one to chose, because they both work for me. thx
First difference, this is the wrong forum for this post! But, since people make honest mistakes... Behind the scenes, there is no difference, at least not in VC++ 6.0. How references are implemented by a compiler is completely up to the compiler, but VC++ uses pointers. For example, if you create a function in a DLL that takes a reference, and you call it from a C program and pass in a pointer, it will still work correctly (at least, using the same builds of VC++ 6.0). As far as usage, references are only available in
C++
, not inC
. Pointers have to be manually dereferenced in order to modify the item that it points to. References do this automatically, so the syntax of using them is different. For example:int FuncA( int &iTest ) { iTest = 10; // Modifies Variable Passed In As "iTest" *iTest = 10; // Wrong - References Do Not Need To Be Dereferenced Like Pointers return; } int FuncB( int *piTest ) { *piTest = 10; // Modifies Variable Pointed To By piTest piTest = 10; // Wrong - Modifies The Location The Pointer Points To return; }
Think of a reference as an "atomatically-dereferencing" pointer. Barring IntelliSense or a similar coding aid, when you use references, it becomes easy for someone else to call the function not realizing that their variable may be modified by the function. When using pointers, they have to take the extra step to pass in the address of the variable, so they have an idea of what is going to happen (assuming non-
const
here). Additionally, you can pass in aNULL
pointer to a function, to indicate "nothing", but a reference has to refer to a real instance of something. For example, you can have anint
pointer with a value ofNULL
, which means a pointer to nothing/nowhere, but you cannot have a reference that refers to nothing (barring some sneaking coding tricks). References also have to be initialized, pointers do not:int iValue; // Bad Habit, But Works int *piValue; // Bad Habit, But Works int &riValue; // Wrong - Will Not Compile At All
There are a lot more details available on pointers and references beyond what I have written here... Peace! -=- James
-
First difference, this is the wrong forum for this post! But, since people make honest mistakes... Behind the scenes, there is no difference, at least not in VC++ 6.0. How references are implemented by a compiler is completely up to the compiler, but VC++ uses pointers. For example, if you create a function in a DLL that takes a reference, and you call it from a C program and pass in a pointer, it will still work correctly (at least, using the same builds of VC++ 6.0). As far as usage, references are only available in
C++
, not inC
. Pointers have to be manually dereferenced in order to modify the item that it points to. References do this automatically, so the syntax of using them is different. For example:int FuncA( int &iTest ) { iTest = 10; // Modifies Variable Passed In As "iTest" *iTest = 10; // Wrong - References Do Not Need To Be Dereferenced Like Pointers return; } int FuncB( int *piTest ) { *piTest = 10; // Modifies Variable Pointed To By piTest piTest = 10; // Wrong - Modifies The Location The Pointer Points To return; }
Think of a reference as an "atomatically-dereferencing" pointer. Barring IntelliSense or a similar coding aid, when you use references, it becomes easy for someone else to call the function not realizing that their variable may be modified by the function. When using pointers, they have to take the extra step to pass in the address of the variable, so they have an idea of what is going to happen (assuming non-
const
here). Additionally, you can pass in aNULL
pointer to a function, to indicate "nothing", but a reference has to refer to a real instance of something. For example, you can have anint
pointer with a value ofNULL
, which means a pointer to nothing/nowhere, but you cannot have a reference that refers to nothing (barring some sneaking coding tricks). References also have to be initialized, pointers do not:int iValue; // Bad Habit, But Works int *piValue; // Bad Habit, But Works int &riValue; // Wrong - Will Not Compile At All
There are a lot more details available on pointers and references beyond what I have written here... Peace! -=- James
thx for this info ;) sorry about the wrong forum, but didn't know where to post it, i didn't find a c++ forum.
-
thx for this info ;) sorry about the wrong forum, but didn't know where to post it, i didn't find a c++ forum.
The **Visual C++ forum is the place for C++ related stuff. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)** -
what's the difference between: int func(classA & test) and int funct(classA * test) with differences i don't mean, the first is by reference and the second one is a pointer. But what i want to know is, what are the advantages / disadvantages of those 2 ways of working to each other? And when should i use the first way, and when the second way? why am i asking this? because sometimes i'm programming i don't know for which one to chose, because they both work for me. thx
hmm... All the C++ boffins will tell you to use the reference version because it's "safer", and that's their justification for just about everything to do with the language ;) Just remember that you can't reassign references and you can with pointers - this might nudge you towards the pointer version. Unless you have a fairly strange function, there is no performance gain from using one in particular. If you want to emulate the powers-that-be and go in the direction the C++ standards commitee have not-so-subtlely been trying to push us in, use the reference version. Be aware of the possiblity of unseen anonymous objects being created when you pass by reference (if you do indeed have to create a temporary const object) - obviously this isn't possible with pointers and prevents this bit of bloat. I go with references cos company's love to see them in your code. To be honest there's not much to gain from it, but try and stick to one in style. It also stops you having to stick &'s everywhere :)