Why not to use references as class members

Started by
1 comment, last by Darkor 20 years ago
With all the advantages of using references, the natural thing to do is to use references everywhere and this leads to some problems that are easily overcome. For example, if you have a reference as a class member, you have to instantiate it in the constructor. I guess the main reason for this is to avoid having invalid references. So fine, we have make sure that we always have a valid reference, we can work with that. Secondly, when we create copy constructors, we''ll have some finger stretching because creating copies with references correctly can be a little tricky. But this is nothing that we can''t solve with a little bit of research. I think those are the two main reasons cited for not using references as class members (I don''t know if there are more). I''ve read that because of this, using pointers is an easier alternative, despite losing the security of references. It is argued that if a class is designed well enough, a class using pointers will be safe enough. Okay question one: A class stores pointers and returns references to client code right? Question two: Class B needs to access the objects from Class A. But Class A only returns references for its objects. So we have to dereference it and do we then store it as a pointer in Class B? Thanks in advance.
Advertisement
quote:Original post by Darkor
For example, if you have a reference as a class member, you have to instantiate it in the constructor.
To be pedantic, (in C++) you have to initialize references in the base initializer list. The constructor body is too late.

quote:I think those are the two main reasons cited for not using references as class members (I don''t know if there are more).
The most obvious one is that references (in C++) are immutable. You can''t change them once they''ve been initialized.

quote:Okay question one: A class stores pointers and returns references to client code right?
That is not a question.

quote:Question two: Class B needs to access the objects from Class A. But Class A only returns references for its objects. So we have to dereference it and do we then store it as a pointer in Class B?
You can''t dereference a reference, despite the name. But class B can use the reference directly, and modify the root object.

Even with pointers, sometimes you have to use a reference to a pointer (if the pointer''s value is to be altered).
quote:To be pedantic, (in C++) you have to initialize references in the base initializer list. The constructor body is too late.


Oh yeah, I was wondering what that was called. But technically, that is still part of the constructor yes? =p

quote:That is not a question


Hmm, okay I shall rephrase =p I meant to ask if that was the standard practice if a class allows access to some data members. They pass a reference because it''s safer yes?

quote:You can''t dereference a reference, despite the name. But class B can use the reference directly, and modify the root object.

Even with pointers, sometimes you have to use a reference to a pointer (if the pointer''s value is to be altered).


Hmm, okay things are a little clearer now, dereferences are used on pointers? So Class B can only store references if Class A returns references only right?

This topic is closed to new replies.

Advertisement