Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [C++] Challenge (https://thetfp.com/tfp/tilted-technology/41711-c-challenge.html)

cowlick 01-16-2004 07:54 AM

[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.

kel 01-16-2004 08:06 AM

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.

n0nsensical 01-16-2004 04:04 PM

You mean like this?

Code:

#include <iostream>
using namespace std;

class CFoo
{
        public:
                void func1() { cout << "func1 for CFoo at " << this << endl; }
                void func2() { cout << "func2 for CFoo at " << this << endl; }
};

typedef void (CFoo::*FooMethod)();

int main()
{
        FooMethod methods[2];
        methods[0] = &CFoo::func1;
        methods[1] = &CFoo::func2;
        CFoo foo;
        (foo.*methods[0])();
        (foo.*methods[1])();
        return 0;
}

What's up with the double spacing on that? I don't know.

cowlick 01-18-2004 03:52 PM

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.

mulletJeb 01-19-2004 01:21 AM

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.

KnifeMissile 01-21-2004 02:49 PM

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...
#define NUM_OF(x)    (sizeof(x)/sizeof(x[0]))

// this will generate a run-time compile error if the parameter is _not_ an array!
template <typename T, size_t size> size_t num_of(T(&)[size]) { return size; }

int main() {
        FooMethod methods[2];
        methods[0] = CFoo::func1;  // like arrays, functions are already pointers, so...
        methods[1] = CFoo::func2;  // ...you don't need to dereference them...

        CFoo foo;
        for(int i = 0; i < num_of(methods); ++i) {
        (foo.*methods[0])();
        (foo.*methods[1])();
        }

        return 0;
}

Functions are already pointers so, like arrays, you don't need to dereference them when assigning to pointers. In fact, I'm surprised that compilers are hacked to work despite the dereferencing!

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!

n0nsensical 01-21-2004 03:07 PM

Quote:

Originally posted by KnifeMissle
Functions are already pointers so, like arrays, you don't need to dereference them when assigning to pointers. In fact, I'm surprised that compilers are hacked to work despite the dereferencing!

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!

I don't know if it's in the C++ Standard but as far as I know the & before function names is simply optional, not illegal. Same for using the * operator to call a function through a pointer. I just copied and pasted in the code and the tabs went with it.

KnifeMissile 01-21-2004 03:23 PM

Quote:

Originally posted by n0nsensical
I don't know if it's in the C++ Standard but as far as I know the & before function names is simply optional, not illegal. Same for using the * operator to call a function through a pointer. I just copied and pasted in the code and the tabs went with it.
It is optional and not illegal, hence my comment on how it's a hack. I can't help but feel that one is more consistent than the other, sort of like this situation:
Code:

const int foo;
int const bar;

Both lines of code do exactly the same thing but bar is more consistent than foo, so I use it despite the popularity of foo's style...


All times are GMT -8. The time now is 06:04 PM.

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73