passing by refrence in differnt ways and other pointer issues...

Started by
20 comments, last by Way Walker 19 years, 8 months ago
ok i'm really not sure on why some people do certain things and have no idea the reason behind them ... for instance ... assuming this class ...

class myClass
{
        int x;
    public:        
        void show (int X)
        {
            x = X;
        }
        void set ()
        {
            cout << x << endl;
        }
        int get ()
        {
            return x;
        }
};


now usually i see passing by refrence like this ... void someFunc (myClass &c) { c.set (100); } ... then in main myClass b; someFunc (b); b.show (); now i see passing by refrence like this ... void someFunc (myClass *c) { c.set (100); } ... main again myClass b; someFunc (&b); b.show (); ... ok so is there any difference here? then sometimes i see pointers used for no real reason ... int sum (myClass a, myClass b) { myClass value; myClass *p = &value p -> set (a.get () + b.get ()); return value; } ... what is the point of using a pointer? do people sometimes only use pointers so they can use the -> operator (assuming it's not overloaded) ... often i cant see why people use pointers when they can just directly access which ever variable they are working on with . operator ... sometimes the use of pointers confuses me ... am i missing something in all of this? thanks for any help.
Advertisement
references are automaticaly dereferenced pointers when you say
int & x=y you must know that the address of x is the address of y
-x=3 means that y=3("x is y")
-you could do it also like this int* x=&y*x=3(by dereferencing x)
-keep in mind that references never "talk about themselves" they always are somebody else
Ohh yes, that explanation was clear as mud. google it's friend, read books, this subject IS WAY TOO BIG to try to explain in few words here.
Quote:Original post by ekrax
ok i'm really not sure on why some people do certain things and have no idea the reason behind them ... for instance ...

assuming this class ...

*** Source Snippet Removed ***

now usually i see passing by refrence like this ...

void someFunc (myClass &c)
{
c.set (100);
}

... then in main
myClass b;
someFunc (b);
b.show ();

now i see passing by refrence like this ...

void someFunc (myClass *c)
{
c.set (100);
}

... main again
myClass b;
someFunc (&b);
b.show ();

... ok so is there any difference here?


very subtle difference, pointers are variables too so it's still pass-by-value but your making copies of address and not the instance it refers to this is called simulated pass-by-reference, don't use pointers for pass-by-references actually use reference types for this it's safe to assume that the instance passed into a function with a reference type as a parameter is the actual instance used. Also you can have a reference to a pointer if you need to actually read/modify the original pointer.

another thing pointers could point to one instance or the first element of an array, or some other dynamic data structure.

Quote:Original post by ekrax
then sometimes i see pointers used for no real reason ...
int sum (myClass a, myClass b)
{
myClass value;
myClass *p = &value
p -> set (a.get () + b.get ());
return value;
}

... what is the point of using a pointer?


Thats is completely pointless & looks nothing more than syntactic sugar for somebody.

Quote:Original post by ekrax
do people sometimes only use pointers so they can use the -> operator (assuming it's not overloaded) ... often i cant see why people use pointers when they can just directly access which ever variable they are working on with . operator ...

sometimes the use of pointers confuses me ... am i missing something in all of this?

thanks for any help.


Yes there are people who just loving using ->.

But there are times where you have to use pointers and there is no way around it, for instance dynamic polymorphism (but you can use references aswell for this), to build dynamic data structures, static polymorphism (STL iterators act like pointers so you can actually use raw pointers aswell), to implement simple callback mechanisms.

EDIT: You may aswell take out dynamic polymorphism for "have to use" as you can use reference types aswell.
Four more reasons someone might use pointers to simulate pass-by-reference in C++:

1) Some feel that using non-const references makes code look like pass-by-value even if its not. Using pointers instead of references when the function modifies the variable's value is a little bit of self-documentation and a little reminder to the reader of the code that it may not come back the same.

2) Can pass a null-pointer which may have some meaning (e.g. use an internal (i.e. static) buffer)

3) Compatibility with C libraries (e.g. need to use arrays instead of vectors)

4) They're a C programmer forced to use C++ and do so out of habit.
Quote:Original post by Way Walker
4) They're a C programmer forced to use C++ and do so out of habit.


5) They're a Java programmer forced to use C++ and can't stand having both pointers and references in the same language. ;) (Java uses '.'s, but their *meaning* is more like '->'. "Java has no pointers", they say, or at least used to say; but that's marketing-speak which is ignorant of the Java object model.)
Quote:Original post by Zahlman
5) They're a Java programmer forced to use C++ and can't stand having both pointers and references in the same language. ;) (Java uses '.'s, but their *meaning* is more like '->'. "Java has no pointers", they say, or at least used to say; but that's marketing-speak which is ignorant of the Java object model.)


Hmm... does anyone know which meaning of "reference" came first? I know Smalltalk's variables behave in much the same way as Java's references, and Smalltalkers use the term "reference" these days, but I'm not sure if they stole that from Java or not. Or some other language? I don't know much about language history :)

In any case, it would've saved me much confusion when I first looked at Java had they said "We don't allow pointer arithmetic" rather than "We don't have pointers" (that, and not having both float and Float).
When passing by reference, the parameter passed is for all intensive purposes the original passed variable. When passing a pointer to a something, you are passing the address of the object, not the actual object itself. On the surface, they would appear to be the same, but they are not. In one instance, you are passing a reference to the object, in the other you are passing the address of the object.

They both provide access to the original object passed, but are used in different ways.

One such example of a difference is a bug in your sample code.
When operating on a pointer to the object, you would have to use the -> operator instead of the . operator. When you pass a reference, you are working on the actual original object, so there is no need for the -> operator.

It should be:
void someFunc (myClass &c){  c.set (100);}void someFunc (myClass *c){  c->set (100);}
We are the music makers and we are the dreamers of the dreams. - WonkaAsking Smart Questions | BookPool
Pointers can be very powerful and dangerous tools. References allow for some of the benefits of pointers without the danger. However references cannot be used in all cases. For instance dynamic memory, or if you want to change the data it points to.

Event though references are pointers "underneath" they are syntactically different, and limited in what they can do.

Use references whenever it makes sense, only use pointers when they are needed.
pointers are references ... period.

In C, passing a non-pointer was called "pass by value" and passing a pointer was called "pass by reference", because in the 50s, 60s, and 70s, when computer science was invented, these two categories of manipulation were discovered to be the only two core concepts needed to describe everything that can be done by the computer ... a value in memory is either the actual value you want to use in an operation, or in some way a "referece" to the value you want to use.

Pointers are memory addresses, and therefore are the most natual form of reference for any computer operation to use ... I mean, your could have written the chip to take letters like 'cs' to refer to the address of the code segment, but the performance cost of making the computer do conversion from human ideas, to the numbers it really understands just doesn't make much sense.

In C++, a reference is just another syntax for using pointers, period. Just like in C++ struct and class are nearly identical and can be emulate each other, so too can everything with references be done with pointers. The only thing that cannot be done with a reference is assigning it to 0 and then checking it for the value 0 (null). A referece can BE null, through accident of programming or mistake, but it is not SUPPOSED to ever be set to an invalid pointer, mainly because it cannot be tested to see if it is null. So when you write something that needs to be able to be null, you MUST use pointers, when you want to pass something which wants the efficiency of pointers, but pass by value semantics, then you should probably use a const reference. For anything in between, the choice is yours.

This topic is closed to new replies.

Advertisement