By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 1,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

STL vector and element constructors

P: n/a
Jan
Hi there,
I've created a reference to a new object (with private copy
constructor)
inside a STL vector

object& obj = aVector[aVector.size()+1]

Is there any possibility to define, which constructor for obj is used?
(In this case, obj() is called, but i want something like obj(xyz) )

Thanks,
Jan

Dec 9 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* Jan:
Hi there,
I've created a reference to a new object (with private copy
constructor)
inside a STL vector
Can't do that; standard collection elements must be copyable.

object& obj = aVector[aVector.size()+1]
Can't do that, either; figure out why (besides, there is a missing
semicolon).

Is there any possibility to define, which constructor for obj is used?
No constructor is used above.

(In this case, obj() is called, but i want something like obj(xyz) )


That's meaningless; please reformulate.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 9 '05 #2

P: n/a
Your code have a bug - it references non existent element. Operator []
does not create new element. Program can crush, constructor (any)
should no be called at all. To create new object using [] your should
use map.

To fix this using vector one can do smth like this:
aVector.resize(aVector.size()+1);
object& obj = aVector.back();
Is there any possibility to define, which constructor for obj is used?

No. The only was is to create custom class which calls proper ctor.
E.g.:
class ProperConstructObject : public object
{
public:
ProperConstructObject()
: object(xyz)
{

}
//...
};
Where xyz should be either global symbol or class member (may be
static).

Dec 9 '05 #3

P: n/a
> object& obj = aVector[aVector.size()+1]

The above does not create a new element for you. What the above does is
to give a new name to the second element following the last element. So
if aVector has 10 elements already, obj would be a new name for the 12th
element in aVector.

Ben
Dec 9 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.