473,789 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 *iShouldNotExis t(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 1215
"James Aguilar" <jf**@cec.wustl .edu> wrote in message
news:d0******** **@newsreader.w ustl.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 *iShouldNotExis t(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 *iShouldNotExis t(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 *iShouldNotExis t(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_funct ion() { /* ... */ }

private:
Object* parent_;
};

int main()
{
Object a;
const Object b(&a);
b.parent()->non_const_func tion();
}

Now b doesn't really own a, so why should modification of it be disallowed?

Jul 23 '05 #4

"Matthias Kaeppler" <no****@digital raid.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
1600
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
2067
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 << " and &s = " << &s << "\n";
1
2005
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 compilable. OTHER INFO: Just so you know what the program does (and so you can give it some input to test it), at this stage I want it to break the input into some parts. Here is the input style: <middle param> <middle param> ...... <middle param>...
18
2380
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 std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >' as `this' argument of `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator(const _Key&) ' discards qualifiers
4
1553
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: ab\r\n"\ "x-0F: ab"
3
5592
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 specified for `const T B<T>::Test() const ' qed.cpp:14: error: overriding `virtual const Foo* A::Test() const'
2
1649
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 code: template< typename T1, typename TExpr, typename T2> inline
8
2213
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 display terrain into screen. I can't display the whitespace characters from terrain.txt file to screen. and It display "weird"( the red color part). http://i19.photobucket.com/albums/b171/NickyEng/display.jpg I tried searching one line by...
4
1621
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 disappears, run: ok 3) uncomment and comment , no problem 4) uncomment and , also no problem #ifndef A_IMPL_H_
1
1201
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 bracket on an if statement and it brought up this error after I corrected it: Here is the code: #include <iostream.h> #include <stdlib.h> #include <fstream> #include <string> #include <iomanip>
0
10408
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, 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...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
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...
1
7529
isladogs
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...
0
6768
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();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
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
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.