gpt4 book ai didi

c++ - 在 C++ 中创建通用对象的最佳方法?

转载 作者:可可西里 更新时间:2023-11-01 18:32:14 26 4
gpt4 key购买 nike

我主要使用 C# 编程,但在谈到 C++ 时,我发现自己不知所措。然而,我需要创建一个 C++ 应用程序,因为它只是更大的 C++ 解决方案中的一个组件。

情况

  • 我有一个包含数据组件(对象)的结构(父级)。这可以是任何类型的数据组件 - 即自定义结构。
  • 只有 Parent 的编译器(创建 Parent incl 对象的人)和最终接收者需要知道对象内部数据组件的类型,因为数据只与它们相关。
  • 然而,Parent 结构可能会通过多个方法、对象甚至其他进程。
  • 对象的数据选项是有限的,编译器和反编译器都知道数据类型的不同选项..因此他们能够反编译原始形式的对象
  • 但是类型可以扩展(即虽然有限但不一定是固定的)并且将来会更新反编译器和编译器

问题- 我不想用"template"重新创建可能遇到此数据组件的每个方法和对象......还看到数据类型将来可能会被更改建议从长远来看遇到对象的每个进程的模板开始时并不理想。

我一直在寻找类似于 C# 对象的东西 - 并编写了以下组件

问题

  • 这是一个很好的解决方案吗?还是我以后会遇到问题
  • 是否可以对此进行改进?我没有考虑过(尤其是不包括 Object\impl.h?
  • 是否有完全不同/更好的解决方案?

代码

  • 我的头文件中有以下内容

    struct Object
    {
    Object();
    // Return true if value has succesfully been set
    // Return false if there is no compatibility between Value and result.
    template <typename T>
    bool GetValue(T &result);
    template<typename T>
    bool SetValue(T value);
    virtual LPVOID GetObjectAddress() = 0;
    virtual const char* GetType() = 0;
    };

    template<typename T>
    struct ObjectType:public Object
    {
    ObjectType(T value);
    T Value;
    LPVOID GetObjectAddress();
    const char* GetType();
    };

我还有一个 CreateType 函数,用于在开始时创建几个现成的对象,例如,在 .h 文件末尾调用如下

    template class CreateType<int>;

请记住,int 只是一个例子……它实际上是不同的结构。

  • 我还有另一个头文件,它包含在这个头文件的底部,如下所示:

    #include "Implementation\Object_impl.h"
    -> looks like this

    template<typename T>
    ObjectType<T>::ObjectType(T value)
    {
    Value = value;
    };

    template <typename T>
    // Return true if value has succesfully been set
    // Return false if there is no compatibility between Value and result.
    bool Object::GetValue(T &result)
    {
    if (typeid(result).name() == GetType())
    {
    result = *(T *)GetObjectAddress();
    return true;
    }
    return false;
    };

    template<typename T>
    bool Object::SetValue(T value)
    {
    if (typeid(T).name() == GetType())
    {
    *(T*)GetObjectAddress() = value;
    return true;
    }
    return false;
    };

    template<typename T>
    const char* ObjectType<T>::GetType()
    {
    return typeid(Value).name();
    }
    template<typename T>
    EXPOBJ LPVOID ObjectType<T>::GetObjectAddress(){
    return (LPVOID)&Value;
    }

我希望我可以将大部分内容包含在一个 cpp 文件中,但是那样我就无法按需创建不同的对象...我还不确定这会产生什么影响...还要扩展类型一个只需要包含普通的对象头文件。

我知道内联是一种选择,但我也认为它不理想?

然而,目前它可以作为“通用”选项编译并完美运行。还可以通过从“Object”继承来进行扩展吗?

...哦,为了使用,我只是这样做 - 这似乎有效:

    Object * a;
a = new ObjectType<testing>(testing());
testing x = testing();
x.i = 50;
a->SetValue(x);
testing y = testing();
testing &z = y;
a->GetValue(z);

cout << z.i << " for z and y = " << y.i << endl;

Result -> 50 for z and y = 50

最佳答案

通常,我们会避免像对象类型这样的远程对象。如果绝对必要(而且几乎从来没有必要),我们使用 boost::any。

至于您的代码:这实际上非常好,但这里有一些我要提到的地方:

你没有析构函数。这是一个主要错误。

virtual ~Object() {}
//and
virtual ~ObjectType() {}

此外,GetObjectAddress 不是类型安全的。

class Object {
//stuff
template<class T>
T* GetObjectAddress();
private:
virtual LPVOID GetRawPointer() = 0;
};
template<class T>
inline T* Object::GetObjectAddress()
{
if (typeid(T).name() == GetType() || typeid(T).name()==typeid(void).name())
{
return static_cast<T*>(GetRawPointer());
}
return nullptr;
}

此外,我会禁止复制和移动赋值位,这有助于防止错误。

class Object {
Object(const Object&) = delete;
Object(Object&&) = delete;
Object& operator=(const Object&) = delete;
Object& operator=(Object&&) = delete;
//stuff
};

我会给派生类型一个默认构造函数和转换构造函数

template<typename T>
struct ObjectType:public Object
{
ObjectType() {}
//C++11 here:
template<class...Ts>
ObjectType(Ts...Vs)
:Value(std::forward<Ts>(Vs)...) {}
//C++03 here:
template<class first>
ObjectType(const first& f) : Value(f) {}
template<class t0, class t1>
ObjectType(const t0& p0, const t1& p1) : Value(p0, p1) {}
template<class t0, class t1, class t2>
ObjectType(const t0& p0, const t1& p1, const t2& p2) : Value(p0, p1, p2) {}
//etc etc etc

最后,使用智能指针代替原始拥有指针。原始指针很好,只要它们不拥有它们指向的东西。

std::unique_ptr<Object> a;
a.reset( new ObjectType<testing>() );

关于c++ - 在 C++ 中创建通用对象的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24815152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com