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. 10 4887
* Al************@gmail.com: 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.
--
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?
<Al************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com... 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<int>& getV() const { return _v; }
// 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
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?
Mike Wahler wrote: <Al************@gmail.com> wrote in message news:11*********************@g14g2000cwa.googlegro ups.com... 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<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 // 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
<Al************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... 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. Al************@gmail.com wrote: 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
Mark P wrote: Mike Wahler wrote: 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.
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dietmar Kuehl wrote: Mark P wrote: Mike Wahler wrote: 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.
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.
Refer a C++ textbook about const section will help you a lot.
yangc wrote: 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
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steve |
last post by:
Hello (and a happy new year)
I'm quite new to C++ and have to programm something for school and can't
get my head around
a couple of things, but at the moment this one is the most important for...
|
by: Alex Vinokur |
last post by:
Testsuites "Comparative Performance Measurement. Reading file into
string" at
http://groups.google.com/group/perfo/msg/8273f4d1a05cfbd1
http://groups.google.com/group/sources/msg/27a9b6f91239c909...
|
by: Jose Fernandez |
last post by:
Hello.
I'm building a web service and I get this error.
NEWS.News.CoverNews(string)': not all code paths return a value
This is the WebMethod
public SqlDataReader CoverNews(string Sport)...
|
by: manokumar |
last post by:
hiye, i notice that some if not all of my folders in winxp
pro. are set as read only and its giving me some problem
with development. so as the natural thing, i unchecked the
read only option and...
|
by: Andrew Brampton |
last post by:
Hi,
This may sound a odd question, but I wanted to know how you return a list of
data from a function. These are some of the ways I know how, and I was
wondering which method you normally use....
| |
by: SpiralCorp |
last post by:
int divide (int a, int b)
{
int r;
r=a/b;
return (r);
}
int main ()
{
int result = divide (20,4);
cout << result
|
by: xicloid |
last post by:
I'm making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I...
|
by: Dan Tallent |
last post by:
A textbox has a attribute for ReadOnly. This seems like such a simple
concept. When a textbox is set to read only the user cannot change the
contents of the field.
I have been trying to find...
|
by: fabian.lim |
last post by:
Hi All,
Im a newbie to C++, I am trying to customize the vector template
STL to a template Class. My code is shown below. Im confused about
something and maybe somebody here might be able to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |