 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Classes with reference members in a vector. |
|
|
Hi all,
I am trying to make a vector containing objects the have a reference
member. However, as soon as I try to push_back an element into this
vector, g++ balks at the fact that it needs to instantiate an operator=
on the class containing the reference members (see below for what I try
to do).
#include <vector>
class TestClass {
private:
const int & m_q;
public:
TestClass(const int & q) { m_q = q; }
};
int main()
{
std::vector<TestClass> v;
int hello = 5;
v.push_back(TestClass(hello));
}
I would have expected that a copy constructor call would be executed in
the offended line instead of an assignment.
Can anyone explain how the desired operation could be implemented (or
why it doesn't make sense for me to want it to work?).
Best regards, Sidney |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Re: Classes with reference members in a vector. |
|
|
Vikram wrote:
| Quote: | I dont think you can have references inside a STL container (like
Vector). Mainly because of issues while copying. A reference, unlike a
pointer, can only be set once.
|
That could well be an STL design decision or limitation that I am not
aware of. Note, however, that at no point I want to set a reference
more than once. |
|
| Back to top |
|
 |
Kirit Sælensminde Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Re: Classes with reference members in a vector. |
|
|
sidney (AT) jigsaw (DOT) nl wrote:
| Quote: | Vikram wrote:
I dont think you can have references inside a STL container (like
Vector). Mainly because of issues while copying. A reference, unlike a
pointer, can only be set once.
|
This is not true. A reference can be set many times unless it is const.
You must however assign a value to it when it is constructed.
| Quote: |
That could well be an STL design decision or limitation that I am not
aware of. Note, however, that at no point I want to set a reference
more than once.
|
In order to grow the vector often a new one is needed. The code then
allocates the new vector and copies the old members into it - this will
use an assignment.
Depending on the implementation and how you use it in the rest of your
code you may be able to get away with a std::list. To get some proper
advice though we'd need to know a lot more about what you want to
achieve.
K |
|
| Back to top |
|
 |
Vikram Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Re: Classes with reference members in a vector. |
|
|
Kirit Sælensminde wrote:
| Quote: | sidney (AT) jigsaw (DOT) nl wrote:
Vikram wrote:
I dont think you can have references inside a STL container (like
Vector). Mainly because of issues while copying. A reference, unlike a
pointer, can only be set once.
This is not true. A reference can be set many times unless it is const.
You must however assign a value to it when it is constructed.
|
Sorry..but how? I am not aware of any way to re-assign a reference
variable. Infact, Scott Meyer's "More effective C++" has that as the
1st item.
Maybe I am missing something here
--Vikram |
|
| Back to top |
|
 |
Vikram Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Re: Classes with reference members in a vector. |
|
|
sidney (AT) jigsaw (DOT) nl wrote:
| Quote: | Oops. In the previous mail, please read ...
TestClass(const int & q): m_q(q) { }
... for the constructor definition. It should be immediately
initialized, of course.... But the problem remains.
Cheerio,
Sidney
|
I dont think you can have references inside a STL container (like
Vector). Mainly because of issues while copying. A reference, unlike a
pointer, can only be set once.
HTH,
Vikram |
|
| Back to top |
|
 |
Frederick Gotham Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Re: Classes with reference members in a vector. |
|
|
Vikram posted:
| Quote: |
Kirit Sælensminde wrote:
sidney (AT) jigsaw (DOT) nl wrote:
Vikram wrote:
I dont think you can have references inside a STL container (like
Vector). Mainly because of issues while copying. A reference,
unlike a pointer, can only be set once.
This is not true. A reference can be set many times unless it is
const. You must however assign a value to it when it is constructed.
Sorry..but how? I am not aware of any way to re-assign a reference
variable. Infact, Scott Meyer's "More effective C++" has that as the
1st item.
Maybe I am missing something here
|
I presume, (although I don't know if he meant it in a smart-alaky way),
that he mean that you can do:
int i;
int &r = i;
int a = 1, b = 2, c = 3, d = 4;
r = a; r = b; r = c; r = d;
However, any C++ programmer who's out of diapers will tell you that
you're setting the value of "i", NOT changing the object to which the
reference refers.
If you ever change the object to which a reference refers, I'll give you
my house, and also eat my hat for good measure.
--
Frederick Gotham |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Re: Classes with reference members in a vector. |
|
|
Kirit Sælensminde wrote:
| Quote: | In order to grow the vector often a new one is needed. The code then
allocates the new vector and copies the old members into it - this will
use an assignment.
|
Why does it allocate (calling a constructor), then assign? To me it is
not inconceivable that one could gop around this, by having the
push_back() using the copy constructor for combined allacation and
assignment.
| Quote: | Depending on the implementation and how you use it in the rest of your
code you may be able to get away with a std::list.
|
I need random access via operator[], so a vector is needed.
| Quote: | To get some proper advice though we'd need to know a lot more about what you want to
achieve.
|
I have a Class that contains references to items that are managed by a
different class, i.e., the Class isn't responsible for their
deallocation, and it may assume that the referenced items exist during
its lifetime, furthermore, I don't need the ability to set them to NULL
- in fact, I would like the help of the compiler to make sure that
doesn't happen.
Currently I just use pointer members but IMHO it would be preferable to
turn the members into references because of these guarantees - the
referred members /are/ properly thought of as aliases of the objects
themselves, instead of pointers to them, making references the natural
choice.
So basically all would be swell if I could just put an instance of
these classes in a vector, which is a-priori not much to ask. It could
be a genuine limitation of the STL but I would be interested if there
were a deeper reason that what I want would be bad.
I don't see an a-priori reason why one /can/ have a class C with
reference members, but one /cannot/ have a usable std::vector<C>. This
issue is independent of my particular problem in my particular program,
it is more a generic question about understanding why this seems to be
a problem with STL or C++. |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Jun 28, 2006 9:10 am Post subject: Re: Classes with reference members in a vector. |
|
|
Oops. In the previous mail, please read ...
TestClass(const int & q): m_q(q) { }
.... for the constructor definition. It should be immediately
initialized, of course.... But the problem remains.
Cheerio,
Sidney |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|