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.

Nickolay
  • 28,261
  • 8
  • 94
  • 160
Michael
  • 11
  • 3
  • 2
    Possible duplicate of [How can I add reflection to a C++ application?](https://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application) – Kaz Jun 24 '19 at 23:34
  • 1
    How can a function that's declared as returning nothing (`void`) return something? – tadman Jun 24 '19 at 23:38
  • My bad, shoud be an type of sting. – Michael Jun 24 '19 at 23:39
  • Hint: It's C++. You can't really dynamically call anything, it's not a dynamic language. You can make a dispatcher function, you can use preprocessing, but you can't really call arbitrary functions based on string names. – tadman Jun 24 '19 at 23:39
  • 1
    If you can store these properties as a `std::map` then you could have a generic `get(const std::string& name)` accessor and special ones like `getName() { return property["name"] }` – tadman Jun 24 '19 at 23:40
  • Your example with using a `std::string animal` makes no sense since that's a string and it will never respond to `getname()`. – tadman Jun 24 '19 at 23:41
  • 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. – Indiana Kernick Jun 25 '19 at 00:08

2 Answers2

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';
}
Indiana Kernick
  • 4,485
  • 2
  • 19
  • 40
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?

Nickolay
  • 28,261
  • 8
  • 94
  • 160
  • As @Kaz mentioned in the comments, the question itself is a possible duplicate. – YatShan Jun 24 '19 at 23:54
  • Obviously, I disagree that this is an exact duplicate of that question. I also feel the other question is too unspecific. – Nickolay Jun 24 '19 at 23:57
  • 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. – YatShan Jun 25 '19 at 00:20