IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 2 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Its counterintuitive
const is part of the type signature of the method. This is why you can overload it. You can think of the const in this case as applying to the 'this' argument. More to the point, you can call this method through a const reference to the object. Methods not marked const cannot be called through a const reference.

This bit of overloading is particularly evil because the type of your reference actually affects the protocol that the object exposes. IOW, simply referring to the object through a const reference causes all of the methods marked const to become dominant and you get different behavior. VERY evil.

Here's another way to think of it. Ignoring the virtual dispatch machinery for a second,
[code]
class TextView {
public:
bool IsEmpty() const;
};
[/code]

strip away the syntax sugar and

[code]
t->isEmpty()
becomes
isEmpty(t);
[/code]

where isEmpty is defined as:

bool isEmpty(const TextView* this);

you can also do this:
[code]
class TextView {
public:
bool IsEmpty() const;
bool IsEmpty(); // non-const version
};
[/code]

so you end up with the two overloaded functions

bool isEmpty(const TextView* this);
bool isEmpty(TextView* this);

and which one gets called depends on the type of the object reference.

A LOT of C++ code gets this wrong (most programmers are unable to grasp this concept fully) and is thus completely unusable for anything but the narrow set of circumstances that the author imagined.



[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
Expand Edited by tuberculosis Aug. 15, 2006, 01:03:04 PM EDT
New Say WHAT?!?
Todd opines:
IOW, simply referring to the object through a const reference causes all of the methods marked const to become dominant and you get different behavior. VERY evil.


Nothing could be further from the truth.

The behavior of a const function (or a non-const function, for that matter), never changes based on the const-ness of the object on which it is invoked. The functions act the same. The only difference is that the compiler simply will not allow you to invoke the non-const functions on a const object. Period. Rather straightforward, actually.

And what, exactly, is a "dominant function"? Don't recall seeing that phrase used in the ANSI standard....

What is true in Todd's missive is the statement "A LOT of C++ code gets this wrong (most programmers are unable to grasp this concept fully)...." and the pathological example Todd cites is one that is frought with peril (for the reasons that he states; you may not invoke the version that you wanted to under certain circumstances).
jb4
"So don't pay attention to the approval ratings that say 68% of Americans disapprove of the job this man is doing. I ask you this, does that not also logically mean that 68% approve of the job he's not doing? Think about it. I haven't."
Stephen Colbert, at the White House Correspondent's Dinner 29Apr06
New You can overload on const
class F
{
public:
void foo() { cout << "Normal" << endl; }
void foo() const { cout << "Const" << endl; }
};

void func(Foo& f) { f.foo(); }
void gunc(const Foo& f) { f.foo(); }

F f;

func(f); // prints 'Normal'
gunc(f); // prints 'Const'

Yeah, that's easy to keep track of.



[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
Expand Edited by tuberculosis Aug. 15, 2006, 04:47:41 PM EDT
New Why the hell would it do / allow that?
Would anyone like to jump in and say that's not brain damaged? Anyone?
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Bell labs has an unlimited crack budget
It has its uses. Here's one.

class G {}

class F
{
G _g; // private member g
public:
G& g() { return _g; } // if we are not const, then neither is our G
const G& g() {return _g; } // if we are const, so is our G
};

So the rule is, if the F is const then you can't mess with the G. But if its not, than you can mess with the G.

Notice that I have to write a bunch of boilerplate to extend the const bubble around the F to cover the G by overloading the g member on const. This is what I dislike about C++. You get this idea that the world should be surrounded by safety rails and you spend your whole life filling in the gaps to make sure that the safety rails cover as much ground as possible - all at the expense of writing code that just does something.




[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
New The easiness to keep track of is not the issue
The issue is your assertion that behavior of the function changes based on the const-ness of the object, which is just plain not true.
jb4
"So don't pay attention to the approval ratings that say 68% of Americans disapprove of the job this man is doing. I ask you this, does that not also logically mean that 68% approve of the job he's not doing? Think about it. I haven't."
Stephen Colbert, at the White House Correspondent's Dinner 29Apr06
New Huh?
You get a different implementation of the method depending on whether the reference to the object is const or not const. So the "apparent behavior" of the function changes.

That's all I'm saying.



[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
New No...
You get the implementation you specify. Every time. No exceptions. That implementation does not change because the object is const or not. The code is the code, and it doesn't magically change because you invoked one overload vs. another.
jb4
"So don't pay attention to the approval ratings that say 68% of Americans disapprove of the job this man is doing. I ask you this, does that not also logically mean that 68% approve of the job he's not doing? Think about it. I haven't."
Stephen Colbert, at the White House Correspondent's Dinner 29Apr06
New I think you're talking past each other...
Because to me it looks like you're both saying the same thing.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New And you specify that by....
the type of the reference to the object. Which can change. Which kind of makes it the anti-polymorphism.

But I think Scott is right. 'nuff said.



[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
New We'll leave it at that.
But it's important to be clear (and not sarcastic) when trying to inform a newbie. YMMV....
jb4
"So don't pay attention to the approval ratings that say 68% of Americans disapprove of the job this man is doing. I ask you this, does that not also logically mean that 68% approve of the job he's not doing? Think about it. I haven't."
Stephen Colbert, at the White House Correspondent's Dinner 29Apr06
New I wasn't trying to be sarcastic
But I know of no allegedly OO language where the method implementation lookup depends on the type of the reference you hold. It runs counter to how polymorphism is supposed to work in which the object responds based on the type of the object and not the type of the reference. So in that respect, C++ is just weird in OO world (and thus, counterintuitive).

Gotta warn the newbies.



[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
New No need to specify OO in this case
So in that respect, C++ is just weird in OO world (and thus, counterintuitive).

That sentence stands on it's own with 'in OO world' removed.

Jay
New What does the type of the reference have to do with anything
The type of reference to the object is effectively the type of the object. There is no difference between having a const reference to an object in C++ and having a const object.
jb4
"So don't pay attention to the approval ratings that say 68% of Americans disapprove of the job this man is doing. I ask you this, does that not also logically mean that 68% approve of the job he's not doing? Think about it. I haven't."
Stephen Colbert, at the White House Correspondent's Dinner 29Apr06
New Nominating this for LRPD (new thread)
Created as new thread #265300 titled [link|/forums/render/content/show?contentid=265300|Nominating this for LRPD]
lincoln

"Chicago to my mind was the only place to be. ... I above all liked the city because it was filled with people all a-bustle, and the clatter of hooves and carriages, and with delivery wagons and drays and peddlers and the boom and clank of freight trains. And when those black clouds came sailing in from the west, pouring thunderstorms upon us so that you couldn't hear the cries or curses of humankind, I liked that best of all. Chicago could stand up to the worst God had to offer. I understood why it was built--a place for trade, of course, with railroads and ships and so on, but mostly to give all of us a magnitude of defiance that is not provided by one house on the plains. And the plains is where those storms come from." -- E.L. Doctorow


Never apply a Star Trek solution to a Babylon 5 problem.


I am not merely a "consumer" or a "taxpayer". I am a Citizen of the United States.


[link|mailto:bconnors@ev1.net|contact me]
     C++ question on const - (ChrisR) - (24)
         Don't change the object, I believe - (JayMehaffey) - (2)
             Doh. - (ChrisR) - (1)
                 More than that... - (jb4)
         Re: C++ question on const - (tuberculosis) - (20)
             I still don't understand... - (ChrisR) - (19)
                 Const is not always forced - (JayMehaffey)
                 Its counterintuitive - (tuberculosis) - (14)
                     Say WHAT?!? - (jb4) - (13)
                         You can overload on const - (tuberculosis) - (11)
                             Why the hell would it do / allow that? - (drewk) - (1)
                                 Bell labs has an unlimited crack budget - (tuberculosis)
                             The easiness to keep track of is not the issue - (jb4) - (8)
                                 Huh? - (tuberculosis) - (7)
                                     No... - (jb4) - (6)
                                         I think you're talking past each other... - (admin)
                                         And you specify that by.... - (tuberculosis) - (4)
                                             We'll leave it at that. - (jb4) - (3)
                                                 I wasn't trying to be sarcastic - (tuberculosis) - (2)
                                                     No need to specify OO in this case - (JayMehaffey)
                                                     What does the type of the reference have to do with anything - (jb4)
                         Nominating this for LRPD (new thread) - (lincoln)
                 Re: I still don't understand... - (jb4) - (2)
                     Which brings up another question. - (ChrisR) - (1)
                         Pure virtual functions - (tuberculosis)

Where do you live? Right here.
294 ms