Casey Patton
Casey Patton

Reputation: 4091

C++: Using a string parameter passed in to access something in a type

My goal is to access a class that is passed in as a parameter inside of myFunction.

Here's what I'm trying to do:

void myFunction(string myString)
{
   callFunctionOn(OuterType::InnerType::myString);
}

I'm trying to call some function on something that's in a type. For example, my code in some other file might look like:

namespace OuterType {
namespace InnerType {
//stuff here
}
}

However, using myString in that way doesn't work. If myString holds the value "class1", then I want that callFunctionOn part to be interpreted as

callFunctionOn(OuterType::InnerType::class1);

I feel like this is super simple, but I've been programming all day and my mind grows tired...

SOLVED: It looks like in order to this in this way, I'd need a language with reflection. To solve this I took a different approach to the problem and passed in a pointer to the class instead.

Upvotes: 0

Views: 152

Answers (4)

Ben Voigt
Ben Voigt

Reputation: 283684

C++ doesn't have reflection built in, but it does have pointers to data, functions, and class members. So you can use a std::map or unordered_set to find the pointer with a particular name (you have to add all the name/pointer pairs into the map beforehand).

Your solution is likely to look something like:

namespace Outer
{
    namespace Inner
    {
        void funcA( void ) { std::cout << "called funcA" << std::endl; }

        std::map< std::string, void (*)(void) > members;
    }
}

// in some initialization function
Outer::Inner::members["funcA"] = &Outer::Inner::funcA;


// later
std::string myString = "funcA";
void (*f)(void) = Outer::Inner::members[myString]; // lookup function by name
(*f)(); // call function via its pointer

Of course the type of the pointer will probably need to change to meet your application requirements.

Upvotes: 6

Peter Varga
Peter Varga

Reputation: 109

maybe this idea: operator() can take parameters, wrapping it in a class ine can make calls that are resolved in the overloaded operator() based on its parameters.

    template<typename TypeSig, class InstanceOf, typename NA,typename Args>
class FuncMap {
    public:
        typedef TypeSig  (InstanceOf:: *cbMethod) ( NA, Args );

        FuncMap( InstanceOf & cInst, cbMethod cbM ) : mcInst(cInst) {mcbM = cbM;}

        TypeSig operator() ( NA na, Args args) {return (mcInst.*mcbM)(na, args);}
    private:
        InstanceOf & mcInst;
        cbMethod mcbM;
};

you need to build a map of runtime string values as keys and pointers to instance methods as seen above. i used this for re-dispatch tracing and custom runtime dispatch with lesser than RTTI overhead.

this allows you to have default, if no key found, or other logic as you wish.

Upvotes: 0

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23332

You're trying to access a variable based on a run-time string that contains its name? That's not possible; the names of variables disappear after compilation and linking. (Except insofar as they are kept around to facilitate debugging).

Upvotes: 3

Darcy Rayner
Darcy Rayner

Reputation: 3445

Do you mean :

OuterType::InnerType::callFunctionOn(myString);

Upvotes: 0

Related Questions