Reference parameters vs. pointers
I''ve been reading "Practical C++ Programming" and they discuss reference variables as a way to call byref, are reference variables the same as pointers.
Reference variable
void FunctionThing(float ¶m)
{
}
Pointer
void FunctionThing(float* param)
{
}
"Don't make me come down there..." -GOD
i haven''t actually disassembled it myself, but i''ve heard many times "yes"
the idea is to make function(const CComplexObject&)
so that you just call the function as normal, or the person using your code calls it as usual, and the information is efficiently pasted from your code to thiers - and they can''t twiddle your data. If you want to change something in the object, then it makes more sense (to me) to pass a pointer to it, which implies that you''re going to change it in some manor.
the idea is to make function(const CComplexObject&)
so that you just call the function as normal, or the person using your code calls it as usual, and the information is efficiently pasted from your code to thiers - and they can''t twiddle your data. If you want to change something in the object, then it makes more sense (to me) to pass a pointer to it, which implies that you''re going to change it in some manor.
- 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
I would like to add to this by saying that once a reference is refering to a variable you can not make that reference refer to another variable but a pointer can. Example:
References automatically dereference the pointer for you so it looks like you''re manipulating the object itself and not through a pointer. I.e. obj.m = 5 rather than obj->m = 5 (i.e. (*obj).m = 5) where obj is a reference to an object.
References come in handy when you use overloaded operators and try to do something like this:
It''s late and I hope this is correct if there are mistakes I apologize. I''m learning c++ too you know
my homepage
E-Mail: BlueOrbSoftware@mailcity.com
// reference
int k(45);
int h;
int &rk = k;
rk = 77;
rk = h; // doesn''t work
// pointer
int *p = &k
*k = 88;
int g(4);
p = &g
*p = 44;
References automatically dereference the pointer for you so it looks like you''re manipulating the object itself and not through a pointer. I.e. obj.m = 5 rather than obj->m = 5 (i.e. (*obj).m = 5) where obj is a reference to an object.
References come in handy when you use overloaded operators and try to do something like this:
objD = objA + objB + objC;
// i.e.
objD = objA.operator+(objB.operator+(objC));
// in each case a *this is returned from within CObj& operator+(const CObj &rhs) function. You can also do this:
objA + objB = 10;
// remember the assignment operator i.e. operator=() is used here not the regular = operator that is used with built in types because objB is an object and its assignment operator is called instead. You define whatever the operator=() should do but basically it should check for self assignment i.e. is objB == objB when you have heap allocated objects in objB then if self assignment is true return *this otherwise delete heap allocated objects in objB, allocate new heap blocks with new and finally assign the right hand side i.e. 10 into the heap of objB. Once that''s done then invoke operator+() of objA and add data members of objB to objA.
It''s late and I hope this is correct if there are mistakes I apologize. I''m learning c++ too you know
my homepage
E-Mail: BlueOrbSoftware@mailcity.com
My take on passing by referance. It's better to make it a pointer because there's a visual referance to what is happening if you need to debug your program.
In case you don't know exactly what a referance is, it's basicly making a copy of your variables value and address, so when you use it in your function, it's as if the variable was global to begin with. Referances can only be created once, because it's only a copy of another variable.
If you don't use referances, it makes it a whole lot easier to read your code and other peopls code plus it's easier to visualy catch a pass by pointer method.
That's my 2 cents.
Edited by - WhatEver on October 7, 2000 10:54:56 AM
In case you don't know exactly what a referance is, it's basicly making a copy of your variables value and address, so when you use it in your function, it's as if the variable was global to begin with. Referances can only be created once, because it's only a copy of another variable.
If you don't use referances, it makes it a whole lot easier to read your code and other peopls code plus it's easier to visualy catch a pass by pointer method.
That's my 2 cents.
Edited by - WhatEver on October 7, 2000 10:54:56 AM
I thought I''d bring to your attention an explanation to your code...
int k(45); // do parenthisis work to init arrays? If not, what is this?
int h;
int &rk = k;
rk = 77; /// this works because it thinks it''s an address
rk = h; // doesn''t work //because it''s not an address
//this will work
*rk=h
int k(45); // do parenthisis work to init arrays? If not, what is this?
int h;
int &rk = k;
rk = 77; /// this works because it thinks it''s an address
rk = h; // doesn''t work //because it''s not an address
//this will work
*rk=h
WhatEver:
int k(45); // do parenthisis work to init arrays? If not, what is this?
this is simply the c++ way to initialize a variable
int k = 45; // C way
int k(45); // C++ way
at least i think so i''m digging deep into my memory for this one. I prefer the c way because it''s more clear.
int k(45); // do parenthisis work to init arrays? If not, what is this?
this is simply the c++ way to initialize a variable
int k = 45; // C way
int k(45); // C++ way
at least i think so i''m digging deep into my memory for this one. I prefer the c way because it''s more clear.
"Don't make me come down there..." -GOD
I''m pretty sure Whatever is not correct about how references work. They create a ''hidden'' pointer, they don''t copy any variables'' values... if that were that case, you may as well just pass by value. It would actually take longer to pass by ref then, because you''d have to copy it again, on return, back to its orginal location.
- 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
The most useful feature of reference values as opposed to pointers is in the return values of functions.
If you define a function with a reference return value eg (in a class):
class MyClass {
public:
inline int &GetX() { return m_x; };
private:
int m_x;
};
then you can use the expression pObject->GetX() on the LHS of an expression, e.g.:
MyClass *pObject;
...
pObject->GetX() += 30;
Dave
Edited by - Heraldin on October 7, 2000 8:44:12 PM
If you define a function with a reference return value eg (in a class):
class MyClass {
public:
inline int &GetX() { return m_x; };
private:
int m_x;
};
then you can use the expression pObject->GetX() on the LHS of an expression, e.g.:
MyClass *pObject;
...
pObject->GetX() += 30;
Dave
Edited by - Heraldin on October 7, 2000 8:44:12 PM
The syntax when using references is a lot more simplified, if a little less flexible. I like using references in classes to return objects from member functions or operator overloads:
string& operator+(string& STRING) // concatenates a ''string''
Doing things this way, you never need to use a pointer in siuations like these. References will do just fine and I use them as much as possible because they are simpler than pointers.
I''m not sure if a pointer is the same as a reference when it''s borken down to assembly but it is probably very similar. Which makes references a better choice whenever you can use them instead of pointers.
-=[ Lucas ]=-
string& operator+(string& STRING) // concatenates a ''string''
Doing things this way, you never need to use a pointer in siuations like these. References will do just fine and I use them as much as possible because they are simpler than pointers.
I''m not sure if a pointer is the same as a reference when it''s borken down to assembly but it is probably very similar. Which makes references a better choice whenever you can use them instead of pointers.
-=[ Lucas ]=-
-=[ Lucas ]=-
I explained the referance wrong. The only way I could think to explain it was by using the word copy. The word alias would have best described what I meant.
Here it is straight out of my pointers book:
That makes sence about the "int k(45);". I''ll try it out later tonight to make sure, because that might come in handy, but might avoid it because not everyone would recognize that syntax.
Here it is straight out of my pointers book:
quote: The crucial point to understand here is that a referance is not a copy of the variable in which it refers. It is the same variable, just under a differant name.
That makes sence about the "int k(45);". I''ll try it out later tonight to make sure, because that might come in handy, but might avoid it because not everyone would recognize that syntax.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement