![]() |
|
Passing pointer to function expecting reference |
Rick
Member #3,572
June 2003
![]() |
I can do this right? I mean a reference is really the address, which is what a pointer is. I get an error when I try to do this. TiXmlElement* data; assignProp(data); void assignProp(const TiXmlElement& test);
======================================================== |
da_flo
Member #1,907
February 2002
![]() |
try assignProp(*data); I encounter such a situation very often when I'm using wxWdigets, with all the constant pointers for default values. |
Rick
Member #3,572
June 2003
![]() |
That works, but doesn't that dereference the pointer? If a reference is an address, why would you have to dereference a pointer to be able to pass it along? ======================================================== |
da_flo
Member #1,907
February 2002
![]() |
I am not a specialist. But when I'm doing that I'm thinking that references are only a dirty trick of C++. I mean, if it's an adress, you can do anything you can do with references using pointers instead. The only difference will be that people will have to explicitly reference when passing a variable, and explicitly dereference the variable in the function body. I have the impression that C++ is only playing with names. It works liking a pointer, but you need to use the name of the variable itself to pass it as an argument. References are confusing. I try not to use them too much, I like to know exactly what I'm doing. Thus I love pointers. |
gillius
Member #119
April 2000
|
A reference is not an address. A reference is not a pointer. An array is not a pointer. These are a few of the common misconceptions in C++. A reference is a named alias to a variable, which is why it is safer than a pointer is because unless you work hard at it, a reference always points to a valid object. References also give you the ability to act on the data as a value type rather than as an address, which means you can call operators like so: "a + b", rather than: "a->operator+(b)" as you would be required to do without them. It also allows a nicer alternative to the common technique of passing a pointer into a function for no other reason than to prevent a copy. Gillius |
da_flo
Member #1,907
February 2002
![]() |
Nice gillius. |
Rick
Member #3,572
June 2003
![]() |
Quote: because unless you work hard at it, a reference always points to a valid object. My example above doesn't point to a valid object right? I mean no memory has been set aside for data, meaning it's not valid and any use of test would bomb right? ======================================================== |
gillius
Member #119
April 2000
|
The test wouldn't "bomb" because it can't compile; it's not valid code. Were you to dereference data, then undefined behavior would result. The program could crash at the literal dereference point, or it could crash when assignProp tried to access it (if it did try). Anything is possible, but crashing when assignProp tried to do something with the object is what would happen on most modern compiler and OS combinations. Technically, there is no reason why references have to be implemented as pointers. I would say that often times they actually aren't, because of the optimizer, but the compilers now are smart enough to do the same optimization with pointers as well. The key difference between a reference and a pointer is that a pointer "exists" in that it takes up space and has an address itself and gets put on the stack or heap or whatever. A reference from the standpoint of the language does not exist in that a reference does not take up space, nor can you take the address of a reference. Everything you do to the reference happens to the object instead, where everything you do to a pointer happens to the pointer. Gillius |
Rick
Member #3,572
June 2003
![]() |
Quote: The test wouldn't "bomb" because it can't compile The above compiles. Anyway, this seems to be causing some problems. bool Form::assignProperties(const TiXmlElement& test) { } The problem I have is that test doesn't work. I use the method QueryAttribute() and it says no such member function. Yet if I define a TiXmlElement inside this function it has the member functions I would expect. Any idea why? [EDIT] [EDIT2] bool Form::assignProperties(TiXmlElement* data) data-> doesn't offer what it should. I mean it doesn't offer what TiXmlElement class has in it. [EDIT3] ======================================================== |
|