2

In my C++ project I want to refer to a specific set of functions with strings.

Basically I want to announce certain abilities of a class to a string-array.

Like this:

class Dog{
    public:
        doBark(){}
        doPoop(){}
        doLick(){}
        doWalk(){}
        doRun(){}
        otherFunction(){}
        getAbilities(){}
}

And getActions() should return a string array (or vector) with the function names starting with "do".

In case any one wonders why: In a framework I use I can only use a certain set of types such as strings. My objects should tell the framework what they can do, so that tasks can be planned accord to their ability. Later, strings should be mapped back to functions, about which I already found useful answers on here.

Now my question: Is this even possible? If yes, how?

7
  • 3
    "Now my question: Is this even possible?" No it isn't. C++ doesn't support runtime reflection so far. Commented Feb 18, 2016 at 0:21
  • 1
    It's not possible automatically ala reflection, but you can use function pointers and strings to simulate it to some extent. You can also create some macros to add a bit of magic to the whole thing. It's still not as good as reflection unfortunately. Commented Feb 18, 2016 at 0:23
  • 1
    pretty sure this can't be done at run-time unless perhaps debugging symbols are used. what you could do (or what I would do I should say) is have a tertiary class called something like class ActionGetter { virtual string getActions() = 0; } (of course using proper syntax), inheriting, and overriding the getActions() method on a per class basis. If someone knows of an easier way let's hear it. Commented Feb 18, 2016 at 0:30
  • Ah, "reflection" was what I was looking for. Remembered using it in Objective-C way back when. Too bad that it is not possible in C++. Have to implementing a workaround based on some of your suggestions. Commented Feb 18, 2016 at 0:33
  • This is the kind of thing that traits are used for in C++. Commented Feb 18, 2016 at 1:14

1 Answer 1

5

Remember that function names as well as variable names are only available at compile time. Once a program is compiled into machine code, these names not available without some serious convolutions to load the debug symbols similar to a source-level debugger.

Sign up to request clarification or add additional context in comments.

6 Comments

Only if you discard symbols, otherwise you can, very well, see function names everywhere if you disassemble the binary.
While that's true, @fanton, it's not particularly useful for the OP.
@fanton AFAIK, these symbols are not available to your own program at run time.
@DavidHoelzer: True, but I didn't even have that intention. My intention was to point out that the answer is not that true.
@fanton You definitely have a pedantic point. Hopefully my most recent edit will satisfy you ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.