C++ and Referrences
i am having a little trouble working with referrences dynamically. this won''t work. does anyone know why?
Object& obj = someobject;
obj = anotherobject;
the value of "obj" does not change. meaning obj still referrences the "someobject" object. ahhhhhhhhhhhhhhh.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Why not simply use:
Or am I missing something here?
Object *obj = someobject;obj = anotherobject;
Or am I missing something here?
Perhaps what you want are pointers?
Object *obj = &someobject
obj = &anotherobject
Object *obj = &someobject
obj = &anotherobject
i realize i can use pointers. however, i really want to make my classes us references for extra security. like all the java people keep crying about. that''s all. i guess it''s not possible to reinitialize a reference. thanx anyway.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
When you declare a variable as a reference, you must point it to another variable that it will reference. This is the only object that the variable can reference. You cannot reset the variable the reference is referencing.
Did that make sense? Basically, once you do this:
SomeObject& objectReference = anotherObject;
You cannot change what objectReference references. It will always reference anotherObject.
Hope that helps.
Josh
http://www.jh-software.com
Did that make sense? Basically, once you do this:
SomeObject& objectReference = anotherObject;
You cannot change what objectReference references. It will always reference anotherObject.
Hope that helps.
Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement