Take the following code example:
class Array {
double *m_array;
public:
Array() { m_array = new double[10]; }
double *begin() const {return m_array;}
};
int main() {
const Array a = Array();
double *iShouldNotExist(a.begin());
iShouldNotExist[0] = -1000;
return 0;
}
This code compiles and runs on g++ 3.3.3 with -pedantic and -ansi set. As I
see it, this code allows me to take an object declared as const and mess
around with its private internals because of a method call that promises
that the object will not be changed. What's up with that? I know that the
right way is to return a const double *, but shouldn't this kind of thing be
disallowed?
- JFA1 4 1191
"James Aguilar" <jf**@cec.wustl.edu> wrote in message
news:d0**********@newsreader.wustl.edu... Take the following code example:
class Array { double *m_array; public: Array() { m_array = new double[10]; } double *begin() const {return m_array;} };
int main() { const Array a = Array(); double *iShouldNotExist(a.begin()); iShouldNotExist[0] = -1000; return 0; }
This code compiles and runs on g++ 3.3.3 with -pedantic and -ansi set.
As I see it, this code allows me to take an object declared as const and
mess around with its private internals because of a method call that
promises that the object will not be changed.
Note that:
i) Array::begin (correctly) does not change the "internals" of an Array
object and
ii) iShouldNotExist[0] = -1000;
does not change the value of a.m_array (it can't, since a is declared
const). It does, of course, change the value of the memory location
pointed to by a.m_array. I'm not sure that this qualifies as "messing
around with the private internals" of object a.
What's up with that? I know that the right way is to return a const double *,
indeed
but shouldn't this kind of thing be disallowed?
Probably not...
Regards,
--
Lionel B
James Aguilar wrote: Take the following code example:
class Array { double *m_array; public: Array() { m_array = new double[10]; } double *begin() const {return m_array;} };
int main() { const Array a = Array();
What kind of weird initialisation is that? :)
const Array a;
would be sufficient.
double *iShouldNotExist(a.begin()); iShouldNotExist[0] = -1000; return 0; }
This code compiles and runs on g++ 3.3.3 with -pedantic and -ansi set.
Yes, the code is, at least syntactically, correct.
All fields of 'a' are const, that is, the pointer. That means, you can't
change the address it's holding, but you still can change the content of
the memory it's pointing to.
As I see it, this code allows me to take an object declared as const and mess around with its private internals because of a method call that promises that the object will not be changed. What's up with that? I know that the right way is to return a const double *, but shouldn't this kind of thing be disallowed?
You sort of dig your own hole. You declare a method as const, which
allows the client to mess with the Array internals. That kind of doesn't
make sense. Either declare it non-const, so it may not be invoked on a
const Array anymore, or just take it out, if you don't want the client
to have access to the array internals. It's your responsibility, and
only yours, that the client can't do that if he's not supposed to.
Solution: Don't write methods which return references to internal data,
if you don't want the client to mess with them. It's like blaming the
thief for robbing the bank after you handed him the keys.
--
Matthias Kaeppler
James Aguilar wrote: Take the following code example:
class Array { double *m_array; public: Array() { m_array = new double[10]; } double *begin() const {return m_array;} };
int main() { const Array a = Array(); double *iShouldNotExist(a.begin()); iShouldNotExist[0] = -1000; return 0; }
This code compiles and runs on g++ 3.3.3 with -pedantic and -ansi set. As I see it, this code allows me to take an object declared as const and mess around with its private internals because of a method call that promises that the object will not be changed.
The "private internals" that you refer to are a pointer to double, and that
pointer cannot be modified, since it is returned by value, i.e. a copy of
it is returned.
What's up with that? I know that the right way is to return a const double *, but shouldn't this kind of thing be disallowed?
How does the compiler know what you want? In this case, the object pointed
to is created by the Array object, which probably means that you consider
the Array to own that object. But you could also have something like:
class Object
{
public:
Object(Object* parent = =)
: parent_(parent)
{}
Object* parent() const { return parent_; }
void non_const_function() { /* ... */ }
private:
Object* parent_;
};
int main()
{
Object a;
const Object b(&a);
b.parent()->non_const_function();
}
Now b doesn't really own a, so why should modification of it be disallowed?
"Matthias Kaeppler" <no****@digitalraid.com> wrote in message
news:d0*************@news.t-online.com... Solution: Don't write methods which return references to internal data, if you don't want the client to mess with them. It's like blaming the thief for robbing the bank after you handed him the keys.
I know the solution, but it seemed to me, based on my former understanding
of things, that it should be enforced. Now I see that that is not the case.
- JFA1 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Anon Email |
last post by:
Hey people,
This looks really weird. I can't make sense of it. Is this a mistake?
Can anyone help?
IStack const & GetStack () const;
Cheers,
|
by: Chris Mantoulidis |
last post by:
I see some really weird output from this program (compiled with GCC
3.3.2 under Linux).
#include <iostream>
using namespace std;
int main() {
char *s;
s = "test1";
cout << "s = " << s << "...
|
by: Chris Mantoulidis |
last post by:
PROBLEM: I'm having some weird problems with string::find() (in
ParamGenerate()), but since I'm not sure if that is the source of all
bad output in my program, I'm posting least code that's...
|
by: Joe Laughlin |
last post by:
(file and class names changed to protect the innocent)
g++ -Wall -c file.cpp: In member function `std::string
Some_Class::get_member(const std::string&) const':
file.cpp:46: passing `const...
|
by: Paul Erion |
last post by:
Hi,
I ran across the following #define in some documentation for an
embedded system (I truncated the actual define, but the general form is
intact).
#define PADDING \
"x-00: ab\r\n"\
"x-01:...
|
by: Ton van den Heuvel |
last post by:
Hi all,
why does the following code not compile, and fail with:
qed.cpp: In instantiation of `B<Foo*>':
qed.cpp:40: instantiated from here
qed.cpp:29: error: conflicting return type...
|
by: Hendrik Schober |
last post by:
Hi,
I have some expression template code that I want to
get to work. I had a typo in it and just spent two
hours to find it, because VC7.1 gave a really weird
error message for it.
This is the...
|
by: nickyeng |
last post by:
I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run.
My simple aim is to display the terrain.txt file into the terrain array, and then...
|
by: er |
last post by:
hi,
the code below generates the following behavior. cld someone please
help understand it?
1) clean project + build project generates build errors (see bottom)
2) build a second time, errors...
|
by: Prosdo |
last post by:
Okay, I am writing this program for class and it kind of works. It goes through the motions, but would skip the prompt where I ask if they want to add another customer. So, I realized I missed a...
|
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,...
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
| |