- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我主要使用 C# 编程,但在谈到 C++ 时,我发现自己不知所措。然而,我需要创建一个 C++ 应用程序,因为它只是更大的 C++ 解决方案中的一个组件。
情况
问题- 我不想用"template"重新创建可能遇到此数据组件的每个方法和对象......还看到数据类型将来可能会被更改建议从长远来看遇到对象的每个进程的模板开始时并不理想。
我一直在寻找类似于 C# 对象的东西 - 并编写了以下组件
问题
代码
我的头文件中有以下内容
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
最佳答案
至于您的代码:这实际上非常好,但这里有一些我要提到的地方:
你没有析构函数。这是一个主要错误。
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/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!