![]() |
[C++] Challenge
So recently I watched from a distance as two qualified coworkers tried for maybe 3 hours to figure out this C++ quandry.
In a method, create a table of pointers to non static member functions of class CFoo. Take an instance of CFoo and call out on the member functions from the table. |
Uhmm... that sounds like it is semantically nonsensical.
You should not be able to create pointers to non-static member functions of a class. ASSUMING that the compiler let you do this (it is possible that It might let you create the function pointer anyway even though they are non-static) you can't call them without passing in the this pointer. Non-static member (Static member functions don't need the this pointer because the location of the unique static instance is fixed in memory) functions always have a this pointer that needs to be passed in by the compiler implicitly, when you call the functions from a table your not passing in the *this pointer to the CFoo instance. If the compiler allows you to explicitly pass in CFoo as the first argument to the functions as you call from that table then it might make sense semantically. Semantics of the language isn't everything though. The compiler may expressly forbid you from creating those function pointers cuz it's just sorta fubar. I don't do C++ anymore, anyway why the heck do you want to do this? There has to be another more elegant way, you could just pass a pointer to the instance of CFoo itself to the right spot. |
You mean like this?
Code:
#include <iostream> |
Yes, just like that.
For those that say this is not possible, check your Stroustroup. As far as this being a poor idea in an object oriented language - I'd agree in most cases there are more elegant solutions. |
n0nsensical: you know, if it weren't for the timestamps on this thread, it would look like you were throwing kel's words ("semantically nonsensical") back at him. that amused me, but then again it's 4:21am.
|
I hope I'm not being pedantic but I would have written the last part out like this:
Code:
// this is simple but, unfortunately, this will erroneously compile with a pointer parameter... The for() loop was just for style, as is the num_of() template function. Although far more complicated than the macro, it's safer 'cause the marcro will erroneously compile if you use a pointer instead of an array. The function only makes sense if you're passing in an array of objects... The double spacing is annoying. I would have used the quote block except it removes all leading white space. By the way, n0nsensical, how on Earth did you enter tabs into the text box? Thanks! |
Quote:
|
Quote:
Code:
const int foo; |
All times are GMT -8. The time now is 09:36 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project