Return a 'read only' view of a STL vector attribute

A

Allerdyce.John

Hi,

I have a class with a STL vector as it attribute.
How can I create a method which just return a 'read-only' view? (i.e.
the caller of the function can only read the vector, not write it)?

class A {
private:
vector<int> _v;
public:
vector<int>& getV() { return _v;} // how to make sure the caller can
only read the content of the array?

}

Thank you.
 
A

Alf P. Steinbach

* (e-mail address removed):
I have a class with a STL vector as it attribute.
How can I create a method which just return a 'read-only' view? (i.e.
the caller of the function can only read the vector, not write it)?

class A {
private:
vector<int> _v;
public:
vector<int>& getV() { return _v;} // how to make sure the caller can
only read the content of the array?

}

Read up on 'const'. It's a good idea to _never_ have underscores at the
start of names. Such names are often used by the implementation.
 
M

Mike Wahler

Hi,

I have a class with a STL vector as it attribute.
How can I create a method which just return a 'read-only' view? (i.e.
the caller of the function can only read the vector, not write it)?

class A {
private:
vector<int> _v;
public:
vector<int>& getV() { return _v;}

This function does not modify the object's
state, so should be:

vector said:
// how to make sure the caller can
only read the content of the array?

Return a const reference:

const vector<int>& getV() const
{
return _v;
}



-Mike
 
A

Allerdyce.John

You mention:
This function does not modify the object's
state, so should be:

vector<int>& getV() const { return _v; }

But if i don't put 'const', it still compiles and works. What do I gain
by adding 'const' in the funciton above?
 
M

Mark P

Mike said:
This function does not modify the object's
state, so should be:

vector<int>& getV() const { return _v; }

I realize you amend this further below, but is the definition above
legal? I'd have thought a const member function wouldn't be permitted
to return a nonconst reference to a member.

Mark
 
J

Jim Langston

You mention:
This function does not modify the object's
state, so should be:

vector<int>& getV() const { return _v; }

But if i don't put 'const', it still compiles and works. What do I gain
by adding 'const' in the funciton above?

const in the method above states that that method will not modify the class
it's called on. Which it doesn't. What you gain is correct programming
behavior, and constant correctness. If, in that function, you had tried to
modify the class, it wouldn't compile. It kinda saves you from yourself.
It's also self documentation that when someone looks at this code, they'll
see that this method does not modify the class very easily.
 
B

benben

You mention:
This function does not modify the object's
state, so should be:

vector<int>& getV() const { return _v; }

It's actually

const vector<int>& getV() const {return _v;}

The first const tells the returned value is read-only;
The second const tells that this member function cannot modify the
object state.
But if i don't put 'const', it still compiles and works. What do I gain
by adding 'const' in the funciton above?

Let's say we drop the const keyword (the second one that follows the
closing parenthesis) from the function declaration. Now we can have:

A a;
const A ca;

a.getV(); // ok, a can be modified
ca.getV(); // ERROR, ca cannot be modified


Regards,
Ben
 
D

Dietmar Kuehl

Mark said:
I realize you amend this further below, but is the definition above
legal? I'd have thought a const member function wouldn't be permitted
to return a nonconst reference to a member.

It depends on how the member is declared. If it is just declared as
a normal member, it would indeed be const and you couldn't return
a non-const reference. If the member is declared as 'mutable' you
could still return a non-const reference.
 
M

Michiel.Salters

Dietmar said:
It depends on how the member is declared. If it is just declared as
a normal member, it would indeed be const and you couldn't return
a non-const reference. If the member is declared as 'mutable' you
could still return a non-const reference.

Actually, you can declare a const function returning a non-const&. In
fact,
you can define it as well:

class X {
static int si;
int& foo() const { return si; }
};

A compiler obviously can't reject this. It can only reject the
"obvious" definitions,
but that wasn't how I read the original question.

Michiel.
 
D

Default User

yangc said:
Refer a C++ textbook about const section will help you a lot.

Using the Google tools to include proper quotes and attributions in
your replies will help *you* a lot. See below.


Brian
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top