473,386 Members | 1,943 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.

Weird const-ness troubles

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
Jul 23 '05 #1
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
Jul 23 '05 #2
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
Jul 23 '05 #3
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?

Jul 23 '05 #4

"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
Jul 23 '05 #5

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

Similar topics

11
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,
10
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 << "...
1
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...
18
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...
4
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:...
3
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...
2
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...
8
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...
4
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...
1
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...
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: 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: 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...
0
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
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...

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.