0

I am wondering how can I dynamically call class functions. Let's say I have a class '"dog", which contains function getname(), that returns name of that specific animal. But how could I call function dog::getname() like that but without the "dog" at the beginning I would use std::string animal = "dog", and then somehow do like animal::getname()

I haven't tried anything yet, because I simply have no idea how I could get similar result, or if it's even possible.

class dog {
public:
   static std::string getname() {
      return "Something";
   }
}

class cat {
public:
   static std::string getname() {
      return "Something2";
   }
}

std::string animal = "dog";

And now somehow call the function getname related to the animal which is in the string.

7
  • 2
    Possible duplicate of How can I add reflection to a C++ application? Commented Jun 24, 2019 at 23:34
  • 1
    How can a function that's declared as returning nothing (void) return something? Commented Jun 24, 2019 at 23:38
  • My bad, shoud be an type of sting. Commented Jun 24, 2019 at 23:39
  • 1
    If you can store these properties as a std::map<std::string, std::string> then you could have a generic get(const std::string& name) accessor and special ones like getName() { return property["name"] } Commented Jun 24, 2019 at 23:40
  • 1
    Is the string an absolute necessity or is it part of an incomplete solution to the real problem? This might be an XY-problem. I think you're looking for virtual functions. Commented Jun 25, 2019 at 0:08

2 Answers 2

1

Are you looking for polymorphism?

#include <memory>
#include <string>

// An animal interface for all animals to implement
class Animal {
public:
    virtual ~Animal() = default;

    virtual std::string getName() const = 0;
};

// An implementation of the animal interface for dogs
class Dog final : public Animal {
public:
   std::string getName() const override {
      return "John";
   }
};

// An implementation of the animal interface for cats
class Cat final : public Animal {
public:
   std::string getName() const override {
      return "Angelina";
   }
};

int main() {
    std::unique_ptr<Animal> animal0 = std::make_unique<Dog>();
    std::unique_ptr<Animal> animal1 = std::make_unique<Cat>();
    // You can pass around a std::unique_ptr<Animal> or Animal *
    // just as you would pass around a string.
    // Although, std::unique_ptr is move-only
    std::cout << animal0->getName() << '\n';
    std::cout << animal1->getName() << '\n';
}
Sign up to request clarification or add additional context in comments.

Comments

0

C++ itself doesn't provide reflection, which would let you do this easily -- the class names are stripped away during compilation.

If you really want to call different functions based on a string name, you'd have to store the mapping from the names to the function pointers and use it to invoke the function you want.

Depending on your specific use-case you could do this by hand (see Using a STL map of function pointers) or use more sophisticated solutions from the thread someone already linked to: How can I add reflection to a C++ application?

3 Comments

As @Kaz mentioned in the comments, the question itself is a possible duplicate.
Obviously, I disagree that this is an exact duplicate of that question. I also feel the other question is too unspecific.
It's about the concept of 'reflection' in C++, That'swhy I mentioned in the comment as duplicate. I agree with the unspecific nature of the other question.

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.