0

Possible Duplicate:
How to detect whether there is a specific member variable in class?

I'm adding features to a C++ library. A thing that'd come in handy was to check if a certain member exists in a struct (it exisits depending on the library version - unfortunately there is no "version" parameter in the library).

Here is a simplified example of what I'd like to do:

struct options {
    int option1;
    std::string option2;
    float option3; // might be included or not

    options();
    void random_method();
}

options::options() {
    option1 = 1;
    option2 = "foo";

    if( EXISTS(option3) ) { // Pseudo-Code -> can I do that at all?
        option3 = 1.1;
    }
}
4

1 に答える 1

0

親構造に純粋仮想関数を実装し、子にそれを実装させることができます。

struct Parent
{
  virtual bool has_option(OPTION) const = 0;
};

struct Car : public Parent
{
  bool has_option(OPTION op) const
  {
     bool result = false;
     if (op == MOON_ROOF)
     {
         result = true;
     }
     return result;
   }
};
于 2012-06-28T17:49:21.420 に答える