c++ - 在运行时组装自己的对象

标签 c++ json object

我想像在 JavaScript 中一样在运行时创建对象:

person=new Object();
person.firstname="John";

我要解析 json 并保存到对象。方法将提前知道并编译。 我创建了这个简单的示例,我想知道我的做法是否正确。

通过字符串添加和调用方法;函数 male 和 female 将作为对象的方法添加到对象中。

现在在对象中是属性性别,但稍后我想对函数做同样的事情。通过在 Object 中声明的方法 setPropertygetProperty。这是个好主意吗?

typedef struct method
  {
  (void)(*method_fn)(Object * _this, string params);
  string method_name;
  } method;




 class Object{

 private:
      vector<method> methods;

 public:
     string gender;
     Object(){};
     ~Object(){};
     void addMethod(method metoda);
     bool callMethod(string methodName,string params);

 };


     void  male(Object *_this,string params) {
        _this->gender="Male";
    }

    void female(Object* _this,string params) {
        _this->gender="Female";
    }





 void Object::addMethod(method metoda)
        {
        methods.push_back(metoda);
        }


 bool Object::callMethod(string methodName,string params){

         for(unsigned int i=0;i<methods.size();i++)
            {
                if(methods[i].method_name.compare(methodName)==0)
                    {
                     (*(methods[i].method_fn))(this,params);
                    return true;
                    }
            }
         return false;
     }

使用它是有效的。

int _tmain(int argc, _TCHAR* argv[])
{

    Object *object1 = new Object();
    Object *object2 = new Object();


    method new_method;

    new_method.method_fn=&male;
    new_method.method_name="method1";


    object2->addMethod(new_method);


    new_method.method_fn=&female;
    new_method.method_name="method2";

    object2->addMethod(new_method);


    object1->callMethod("method1","");
    object2->callMethod("method2","");

    return 0;
}

最佳答案

最好使用 map,并使用引用:

class Object {
public:
    Object() {}
    ~Object() {}
    typedef void (*Method)(Object& _this, const std::string& params);
    void setMethod(const std::string& name, Method fn) { methods[name] = fn; }
    bool callMethod(const std::string& name, const std::string& params);

private:
    std::map<std::string, Method> methods;
    std::map<std::string, std::string> properties;
};

bool Object::callMethod(const std::string& name, const std::string& params)
{
    // Look for it.
    // Note: in C++11, you can do this instead:
    //     auto methodIter = methods.find(name);
    std::map<std::string, Method>::iterator methodIter = methods.find(name);

    if(methodIter == methods.end())
        return false;

    // Found it.
    (*(methodIter->second))(*this, params);
    return true;
}

请注意,我添加了 properties,它的功能与 methods 相同,只是它将名称映射到字符串而不是函数。这将替换您的 string gender; 内容。

无论如何,只需对您的主要功能进行一些小改动,即可消除对方法结构的需求:

int _tmain(int argc, _TCHAR* argv[])
{

    Object *object1 = new Object();
    Object *object2 = new Object();

    object1->addMethod("method1", &male);
    object2->addMethod("method2", &female);

    object1->callMethod("method1", "");
    object2->callMethod("method2", "");

    return 0;
}

附言我使用了标准类型名称的 std:: 前缀版本,因为您永远不应该在头文件中使用 using namespace std; 或类似名称。

关于c++ - 在运行时组装自己的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16301943/

相关文章:

c++ - 在 C++ 应用程序和 Web 应用程序之间进行通信

c - 使用 C 解析 JSON

php - 从 Android Studio 的 Mysql 数据库获取数据

javascript - JavaScript中如何检查一个JS对象的所有键和值是否存在于另一个JS对象中?

vb.net - 对象的深拷贝

matlab - 结构域是函数matlab

c++ - 编译器将派生类视为实际定义的抽象引用函数

c++ - 在映射中存储指向成员函数的指针

php - JSON 中的 foreach 函数只返回一行

c++ - 调用 CComModule.RegisterServer、_AtlComModule.RegisterServer 和 LoadTypeLibEx 进行 TypeLib 注册有什么区别?