const vector<A> vs vector<const A> vs const vector<const A>

J

Javier

Hello,
thanks for the replies to my questions.
I have one more:
class A
{
public:
m1() const;
m2();
};

is there any difference between

std::vector<const A> v;
const std::vector<A> v;
const std::vector<const A> v;

and, what about:

A a1;
v.push_back(a1);
v[0].m1()
v[0].m2()

in the three cases?
 
N

Neelesh Bodas

Hello,
thanks for the replies to my questions.
I have one more:
class A
{
public:
m1() const; missing return value
m2(); missing return value;

};
is there any difference between

std::vector<const A> v;
const std::vector<A> v;
v is a const vector whose elements are of type A. Effectively,
v.push_back() (or any other operation that changes the vector) is not
allowed on v
const std::vector<const A> v; doesnot compile

and, what about:

A a1;
v.push_back(a1);
v[0].m1()
v[0].m2()
Talking about the 2nd case (const std::vector<A> v), since v is a
const vector, non-const member functions cannot be called, const
member functions can be.

-N
 
J

James Kanze

thanks for the replies to my questions.
I have one more:
class A
{
public:
m1() const;
m2();
};
is there any difference between
std::vector<const A> v;
const std::vector<A> v;
const std::vector<const A> v;

Yes. The first and third aren't legal. An element of a
container must support assignment, and A const doesn't.

In general, in the standard library, declaring a container to be
const (i.e. your second declaration) means that 1) the topology
(number and order of elements) cannot change, and 2) the value
of the individual elements cannot change.
and, what about:
A a1;
v.push_back(a1);
v[0].m1()
v[0].m2()
in the three cases?

Illegal. As I said above, the declaration of v is illegal in
the first and third cases above. And if v is declared as in the
second case, push_back cannot be used on it. Given something
like:

std::vector< A > const v( 1, A() ) ;

however, v[0].m1() is legal, v[0].m2() no.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top