473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

stl list, const correctness

I have a weird question about const correctness when using an stl list.
I have a wrapper Inventory class that holds a list of pointers to
Items. (Yes, they have to be pointers.)

Now, obviously the Inventory class isn't going to mutate the Items, so
its Add function should be Add(const Item *), and the list should be
std::list<const Item *>. One way to access the Inventory's item's is to
iterate through them with:

std::list<const Item *>::const_iterator ListBegin() const;
std::list<const Item *>::const_iterator ListEnd() const;

However, I obviously would like to mutate the items I get while
iterating through the Inventory's list. What am I doing wrong? My
currently implementation is the above without any const's, but obviously
I would like to encapsulate the fact that Inventory doesn't mutate its
items, and I would mostly like to understand how to do this correctly.

(Please don't try to convince me to use references instead of pointers,
or to not encapsulate std::list. I have my reasons, and I know they're
sound. Unfortunately, they didn't teach us things like const correctness
in school.)
Jul 23 '05 #1
2 2327
"Jim Strathmeyer" <st****@ipass.net> wrote...
I have a weird question about const correctness when using an stl list.
Weird indeed.
I have a wrapper Inventory class that holds a list of pointers to Items.
(Yes, they have to be pointers.)

Now, obviously the Inventory class isn't going to mutate the Items
OK, so, it doesn't mutate them. Good.
, so its Add function should be Add(const Item *), and the list should be
std::list<const Item *>. One way to access the Inventory's item's is to
iterate through them with:

std::list<const Item *>::const_iterator ListBegin() const;
std::list<const Item *>::const_iterator ListEnd() const;

However, I obviously would like to mutate the items
Wait, didn't you just say that the Inventory isn't going to mutate them?
So, which is it? Do you need to mutate them or don't you? Or do _you_
need to mutate them but you don't trust the Inventory to do it? Has it
let you down before? I mean those Inventory objects can be really tricky
to handle...
I get while iterating through the Inventory's list. What am I doing wrong?
So far you're just muddying the waters with "isn't going to" and "would
like to" WRT mutating the same items. You should stick to one of them.
My currently implementation is the above without any const's, but
obviously I would like to encapsulate the fact that Inventory doesn't
mutate its items, and I would mostly like to understand how to do this
correctly.
What happens if you do add 'const', but don't ask for 'const_iterator'?

What happens if you simply add 'const' and let it be? Do you get a compile
error? If so, what error do you get? Perhaps you could distill your code
to the minimal amount where you can demonstrate the actual problem you're
having with const-correctness...
(Please don't try to convince me to use references instead of pointers,
References cannot be stored in a container, so there is no reason for us
to try to convince you to use references. It simply cannot be done.
or to not encapsulate std::list. I have my reasons, and I know they're
sound. Unfortunately, they didn't teach us things like const correctness
in school.)


"Doctor, I am hearing voices inside my head. Don't try to tell me that
I am crazy. I know that I am not crazy. It's just I really hate those
gremlins who found their shelter between my ears." :-)

On a serious note, if you want our help you have to try to trust our
ability to help. That includes stating your "reasons" and testing out
their "soundness". In any case, post more code.

V
Jul 23 '05 #2
Jim Strathmeyer wrote:

However, I obviously would like to mutate the items I get while
iterating through the Inventory's list. What am I doing wrong?
const means "this object will never be modified through this access
path." Since your Inventory type provides an access path for
modification of its elements, the elements are not const.
My
currently implementation is the above without any const's


Right.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
3
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
2
by: Jianli Shen | last post by:
in a *.h file, there is a declaration: const ClassName *functionName() const {return oneVar;} I was confused by the two const there. can anybody help explain why we need the first const....
11
by: Markus.Elfring | last post by:
A couple of software provides const-incorrect programming interfaces. I guess that it is possible to develop const-correct APIs/SDKs from the beginning if a few basic design rules and patterns...
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
34
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
12
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
2
by: Jim Langston | last post by:
In my function/method paramater list I try to keep constant correctness. I.E. I would use: int Foo( const char* a, int b ) instead of int Foo( char* a, int b ) My question reguards int b. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.