References vs Pointers

"Jon Earle" <[email protected]>
Newsgroups gmane.user-groups.linux.ottawa.general
Message-ID <[email protected]>
Hey folks!

Okay... simple one.

I have a test program to illustrate what I am working with.  The class is
fixed - I cannot change its definition.

class myclass
{
public :
    int *i;
};

// receive a reference to an int in the 2nd param.
void setval(myclass obj, int & x)
{
    obj.i = x;
}

int main(void)
{
    myclass mc;
    int j = 10;

    setval(mc, j);

    return 0;
}

If I compile this, I get:

$ gcc -o ptrref ptrref.cpp
ptrref.cpp: In function ‘void setval(myclass, int&)’:
ptrref.cpp:9: error: invalid conversion from ‘int’ to ‘int*’

Now, I was under the impression that references were just a fancy word for
pointers, ie: a reference is an address of an object.  So, why can I not
assign the reference to a pointer variable?  Missing something, not sure
what.

Cheers!
Jon

-- 
OCLUG general discussion list
[email protected]
http://oclug.on.ca/mailman/listinfo/oclug
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.