d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Hard Q: Why Constant Member Function Are Bad Idea? > Fg Reward For Best Argument/explaination
Add Reply New Topic New Poll
Member
Posts: 2,776
Joined: Dec 29 2005
Gold: 21,468.10
Aug 1 2013 12:42am
Contrary to popular believe, convince me by stating why const member functions are bad idea (const-correctness).
This is not a trivial question, as it may require a C++ expert to correctly answer; since only they will know why something that sounds great in text-book is actually useless/flawed….
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Aug 1 2013 01:30am
I don't see why they would be as long as they're used when appropriate but that applies to anything else

This post was edited by SelfTaught on Aug 1 2013 01:32am
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Aug 8 2013 05:19pm
Well essentially cause it's used in large multi threaded projects to protect shared data by casting to it. From a semantic stand point it makes more sense to cast the function argument to a const pointer rather than the function itself. Const function members implies the function is run exclusively on the heap, and it may not modify any members of its class unless they're static. It probably doesn't inherit the internal this pointer either because of that mechanic, as there is no guaranty the class was allocated in the heap or the stack. Most are in the stack however, which is also a problem in heavily multi threaded projects where you have limited heap available, as each thread usually has its own static amount provided at spawn. In such projects you try to stuff as much as you can of your memory needs in the stack.

There's a matter of speed as well. Memory allocation is very resource intensive. Again, with a lot of threads (100+) you'd be giving up quite a lot of cycles to memory management by using anything allocated in the heap, which implies dynamic content, so alot of internal alloc/free. Keep in mind that in most languages, memory allocation implies locking and memory fences. Quite expensive alright. It would be a ressource nightmare if you'd pass entire objects as arguments to a const member function instead of a pointer. This is however profoundly counter intuitive as people tend to do this a lot, thinking "hey, im within my class". As a matter of fact, I see it a lot with operator overloading.

As far as the member part goes, my understanding was that you're expected to use the public/private/protected nomenclature with class inheritance to achieve proper data isolation within your classes, like let's say, to protect a semaphore through polymorphism.

Then again C++ is one of those languages that chooses to allow you the freedom of shooting yourself in the foot rather than forbid certain combinations at the cost of semantics.
Member
Posts: 559
Joined: Apr 7 2008
Gold: 1,030.00
Sep 14 2013 12:51pm
I'm not convinced const member functions are bad but used the wrong way they can definitely behave counter-intuitively. Argument:

If an object has a pointer to something outside of itself, a const member function in that object can still modify the data being pointed to as long as the pointer remains constant. This allows there to be const member functions that do not act very 'const' at all.

Source: Scott Meyers 'Effective C++' (Item 3)

This post was edited by hbfs on Sep 14 2013 12:52pm
Member
Posts: 1,087
Joined: May 2 2005
Gold: 75.00
Oct 2 2013 10:10pm
Top level const
Low level const

Look into these to as I believe these will help with your question.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll