oop - C++ use string to call object member function -
i have superclass entry
, subclasses musicalbum
, book
, film
. instances of these subclasses stored according name of item ie book1
. name , type of these instances stored in vector cat_vector
vector of objects of class libcatalogue
stores name , type:
class libcatalogue{ std::string name; std::string type; public: libcatalogue(std::string name, std::string type); std::string getname(); std::string gettype(); }; libcatalogue::libcatalogue(std::string name, std::string type) :name(name), type(type) {}; std::vector <libcatalogue> cat_vector;
entries in vector made in constructor eg.
musicalbum::musicalbum(std::string a, std::string b, std::string borrower) : name(a), artist(b), entry(borrower){ cat_vector.push_back(libcatalogue(name, "musicalbum"));
each subclass has member function called printdetails()
. want use loop step through each entry in cat_vector
, print details of entry following not work:
int no = 1; (auto = begin(cat_vector); != end(cat_vector); ++it) { std::string name_ = it->getname(); std::string type_ = it->gettype(); std::cout << "entry no. " << no << std::endl; std::cout << "name: " << name_ << std::endl; std::cout << "type: " << type_ << std::endl << std::endl; if (type_ == "musicalbum"){ name_.printdetails(); //print using musicalbum member function } //etc... no++;
i know because name_
string , not object of of classes want call, haven't been able find way convert far. there way tell compiler name_
referring object of 1 of subclasses?
c++ statically typed compiled language.you cannot create variables on fly. fortunately, cases these, work around use lookup table. achieved through map key string , value function want associate , call particular string.
i know because name_ string , not object of of classes want call, haven't been able find way convert far. there way tell compiler name_ referring object of 1 of subclasses?
when qualify member, member name qualified respect type of variable not respect content. call name_.printdetails()
mean trying invoke member function printdetails instance of type std::string std::string not have member function named printdetails.
a simple example extend above idea
struct spam { enum { no_of_functions = 4 }; spam() { lookup_callback["foo1"] = std::bind(&spam::foo1, this); lookup_callback["foo2"] = std::bind(&spam::foo2, this); lookup_callback["foo3"] = std::bind(&spam::foo3, this); lookup_callback["foo4"] = std::bind(&spam::foo4, this); } void foo1() { std::cout << "foo1" << std::endl; } void foo2() { std::cout << "foo2" << std::endl; } void foo3() { std::cout << "foo3" << std::endl; } void foo4() { std::cout << "foo4" << std::endl; } void call(std::string name) { if (lookup_callback.count(name) > 0) { lookup_callback[name](); } else { std::cerr << "invalid function call" << std::endl; } } std::map<std::string, std::function<void(void)>> lookup_callback; }; // driver program test above functions int main() { std::string name; spam spam; (std::cin >> name; name != "quit"; std::cin >> name) { spam.call(name); } }
Comments
Post a Comment