2

Since pointer and reference both achieve the is-a relationship in context of inheritance in c++. So as we are used to store container of pointers to object to achieve polymorphic behavior is it also possible to do it with containers? That is to store references instead of pointers in the container?

My question is different than this because it's in the context of polymorphism

Community
  • 1
  • 1
Hanna Khalil
  • 975
  • 1
  • 10
  • 28
  • Pointers are references I think. Can you explain further your requirement? Or give an example? – Ely Jun 17 '16 at 12:05
  • instead of having set we will have set is such thing possible? – Hanna Khalil Jun 17 '16 at 12:05
  • 6
    @Elyasin - Pointers are *not* references – Smeeheey Jun 17 '16 at 12:06
  • 2
    related/dupe [Why can't I make a vector of references?](http://stackoverflow.com/questions/922360/why-cant-i-make-a-vector-of-references) – NathanOliver Jun 17 '16 at 12:08
  • @Smeeheey Maybe you are just riding on a technicality here. But as far as usage in that use case is concerned I think they are same same. Look at the answer below. The reference_wrapper just wraps a pointer. Look at it that way: `int *p` it a pointer declaration, `p` is a reference and `*p` a dereference. There's no need to show off with tightly defined technical terms. I understand what you want to say, but that is not the way I wanted to go, just leave it. – Ely Jun 17 '16 at 14:14
  • @Elyasin - The answer below is mine. Oh the irony :) – Smeeheey Jun 17 '16 at 14:18
  • Irony on or off. I hope you learned something new today. – Ely Jun 17 '16 at 15:38

1 Answers1

0

You can do it using the special class std::reference_wrapper, for example:

std::vector<std::reference_wrapper<MyClass>> ref_vector;

Here is an example use:

std::vector<int> vec{1, 2};
std::vector<std::reference_wrapper<int>> refs(vec.begin(), vec.end());

vec[1] = 3;
std::copy(refs.begin(), refs.end(), std::ostream_iterator<int>(std::cout, " "));

The output it "1 3", which demonstrates that references to the original elements are held rather than copies (otherwise the output would have been "1 2").

Smeeheey
  • 9,906
  • 23
  • 39
  • Could you, if possible, please provide some examples of use? – Hanna Khalil Jun 17 '16 at 12:09
  • @NathanOliver: quoting from cppreference: "std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object. It is frequently used as a mechanism to store references inside standard containers (like std::vector) which cannot normally hold references." – Smeeheey Jun 17 '16 at 12:10
  • 1
    @Smeeheey And if you look a little farther down in the "possible implementation" it has `T* _ptr;`. Just wrapping a reference would not work as references are not copyable and that would make `reference_wrapper` not copyable. – NathanOliver Jun 17 '16 at 12:11
  • @NathanOliver - I'm not contradicting you. The OP asked for a way that references could be stored in a container, and I showed him the construct that does this. Yes, its is implemented using pointers. But then again references themselves are implemented using pointers anyway. – Smeeheey Jun 17 '16 at 12:18
  • @Smeeheey: References are alias. Whether they are implemented as pointer or not is upto implementer. Pointer have memory location. Reference may or may not have their own dedicated memory location. – sameerkn Jun 17 '16 at 12:39
  • 1
    @sameerkn - I know all that. Sigh... Why am I getting dragged off into this? I'm perfectly aware of the conceptual differences between references and pointers. My above implementation provides a *conceptual* container of references, precisely what `std::reference_wrapper` was designed for. The fact that the *implementation* may or may not use pointers (just like the implementation of references may or may not use pointers) is surely not the point! – Smeeheey Jun 17 '16 at 12:44
  • @Smeeheey: Am not teaching. I saw a line in your comment *"But then again references themselves are implemented using pointers anyway."* which seems to be strongly advocating the fact that references are implemented as pointer. Any way that's all off topic :) – sameerkn Jun 17 '16 at 13:02