Thread: Vector of references

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Vector of references

    I think my understanding of references is incomplete. I would like to create a vector of references to a type, the purpose being to gain the benefits of a vector without having to copy each element or use pointers. Why is it not possible to create a vector of references? (I tried with Borland 5.5.1 on Win32 and got a slew of errors referring to "Cannot define a pointer or reference to a reference in int main()").

    My code:

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class MyTest {
    
    	public:
    	MyTest() {
    		cout << "Creating a new MyTest object!" << endl;
    	}
    	MyTest( const MyTest& ) {
    		cout << "Creating a new MyTest object!" << endl;
    	}
    
    };
    
    int main() {
    
    	MyTest a;
    	MyTest b;
    
    	vector< MyTest& > baz;
    	baz.push_back( a );
    	baz.push_back( b );
    
    }

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Why is it not possible to create a vector of references?
    Because all references must refer to something. The references in your vector don't have anything to alias, so it's an error. Also, the implementation probably uses references too and a reference to a reference is illegal, but a reference to a pointer is not, that's why you can make a vector of pointers.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    just as you cannot create an array of references so you cannot create a vector of them. Use pointers or better still a reference counted smart pointer such as those available in the boost library.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Brighteyes
    Because all references must refer to something. The references in your vector don't have anything to alias, so it's an error.
    Do they not refer to "a" and "b" (MyTest objects above)? As long as the vector is within the same scope as a and b (i.e., they do not fall out of scope as a result of being local), are references to those objects not valid?

    Also, the implementation probably uses references too and a reference to a reference is illegal, but a reference to a pointer is not, that's why you can make a vector of pointers.
    stoned_coder:
    just as you cannot create an array of references so you cannot create a vector of them
    I recognize that this is true, but I'm trying to determine why. I do not see the inherent problem with such a construct (in theory) as long as the objects referred to are still in scope, but I am obviously missing something. Or is it a problem with trying to implement such a thing?

    Perhaps the answer lies in knowing exactly what a reference is? Is it merely a new name for something? A compiler construct that has no actual value? (I.e. an int is a datatype that takes up 4 bytes, an int* is a pointer to such a value, and takes up some other amount of memory, but how much memory and what exactly is an int&? Does it exist in the same way?)

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    According to the c++ standard a reference may be totally compiler implemented and have no storage. No point in making an array if the things you want to put in there actually dont exist.Therefore you may never create an array of references. As luck would have it you do not need to either as pointers are available in c/c++ and in c++ we can also have smart pointers which are objects that act as a pointer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Do they not refer to "a" and "b" (MyTest objects above)?
    Not at first, and references have to be initialized immediately. From the line where you create the vector to the lines where you push references onto it, they alias nothing at all. It doesn't matter whether there's nothing in between those lines or millions of operations, it's still not legal.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler settings, references etc...
    By ejohns85 in forum C++ Programming
    Replies: 0
    Last Post: 05-14-2009, 04:53 AM
  2. VC++ 2005 Express: missing references page
    By psychopath in forum Tech Board
    Replies: 1
    Last Post: 08-21-2006, 04:55 PM
  3. Arrays of references
    By Welshy in forum C++ Programming
    Replies: 16
    Last Post: 07-04-2005, 11:28 PM
  4. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 08:14 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 04:21 PM