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_itera tor ListBegin() const;
std::list<const Item *>::const_itera tor 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.) 2 2346
"Jim Strathmeyer" <st****@ipass.n et> 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<cons t Item *>. One way to access the Inventory's item's is to iterate through them with:
std::list<const Item *>::const_itera tor ListBegin() const; std::list<const Item *>::const_itera tor 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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 stupid to understand
and make them work.
E.g. I have read that boost's smart pointers are able to do this
convertion, but the following code doesn't compile (VC++6.0):
|
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, my compiler doesn't have temporary const objects. Are there any
reasons to have a const reference parameter ?
Thanks in advance for your help
|
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.
why we need the second const here ?
Thanks
|
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 would be considered.
How difficult is it for beginners to make it right and to get used to
this kind of programming style?
How much do you need to fiddle with "const_cast" because the key word
"const" was forgotten for type specifiers in important...
|
by: quantdev2004 |
last post by:
Hi all,
I have been deling with this kind of code:
class Foo
{
public:
void NonConstMethod()
{}
};
| |
by: Perro Flaco |
last post by:
Hi!
I've got this:
string str1;
char * str2;
....
str1 = "whatever";
....
str2 = (char *)str1.c_str();
|
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 argument.
I was hoping to create a map<int, const char*that would be used in
the showError member function of the TExceptionHandler class where the
key (int) would be the error code and const char* the message printed
out to the console.
My question...
|
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 parameter
will not be changed - so don't worry.
2. It tells the implementor and the maintainer of this function that
the parameter should not be changed inside the function. And it is for
this reason that some people advocate that it is a good idea to...
|
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. Would it be more correct to make it
int Foo( const char* a, const int b )
instead? I do know the effect of this would be that I can't change b in the
function, which I normally never do anyway.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |