如何实例化正确的对象类型? [英] How to instantiate the correct object type?

查看:121
本文介绍了如何实例化正确的对象类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写类以生成PDF文件的过程.作为PDF规范的一部分,有许多基本类型-INT,FLOAT,STRING,ARRAY,DICTIONARY.

目前,我已经定义了一个抽象基类pdfObj,所有其他类型都从该基类继承.到目前为止,一切都很好.

数组对象的功能之一是,数组中的每个元素都可以是上述任何对象-包括数组本身.

将对象添加到数组时,我需要在创建该对象的副本并将副本添加到数组之前确定正确的对象类型.虽然我执行的实现有效,但我不禁想知道是否没有我缺少的东西或采用错误的方法.我已附上以下代码:

任何/所有协助将不胜感激.

I''m currently in the process of writing a class to produce PDF files. As a part of the PDF specification there are a number of basic types - i.e INT, FLOAT, STRING, ARRAY, DICTIONARY.

Currently, I''ve defined an abstract base class - pdfObj, from which all of these other types inherit. So far, so good.

One of the features of the array object is that each element in the array may be any of the above objects - including an array itself.

When adding an object to the array I need to determine the correct object type before creating a copy of that object and adding the copy to the array. While the implementation I have works, I can''t help but wonder if there isn''t something I''m missing or approaching the wrong way. I''ve attached the code below:

Any/all assistance will be most appreciated.

void pdfArray::addItem(const pdfObj *newItem)
{
    pdfObj *item;
    if (typeid(*newItem) == typeid(pdfInt))
        item = new pdfInt( (pdfInt&) *newItem);

    if (typeid(*newItem) == typeid(pdfFloat))
        item = new pdfFloat( (pdfFloat&) *newItem);

    if (typeid(*newItem) == typeid(pdfString))
        item = new pdfString( (pdfString&) *newItem);

    *item = *newItem;
    mArray->push_back(item);
}

推荐答案

为什么不对接受任何类型的addItem()方法使用不同的重载?像这样的东西:
Why not use different overloads of the addItem() method which accepts any of the types you have? Something like:
void pdfArray::addItem(const pdfInt *newItem)
{
// ...
}
void pdfArray::addItem(const pdfFloat *newItem)
{
// ...
}
// etc.


刚刚意识到还有一个更好的方法来解决这个问题.

功能模板

唯一的丑陋"是,我必须在头文件中实现功能主体-我不能在H文件中具有定义,而不能在CPP文件中具有实现(不能求助于将cpp文件包含在头文件):mad:

无论如何,最终的函数主体简短而甜美-避免了维护问题,因为我只需要确保函数需要处理的任何新类型都符合两个标准即可:

1.新对象必须来自pdfObj
2.新对象必须具有副本构造函数

最终的代码因此出现在类定义内:

Just realized that there''s a better way to go about this..

Function Templates

The only ''ugly'' thing is that I have to implement the function body in the header file - I can''t have the definition in a H file and the implementation in a CPP file (without resorting to including the cpp file in the header file) :mad:

Anyhow, the final function body is short and sweet - it avoids maintenance issues since I just need to ensure that any new type that the function needs to deal with conforms to two standards:

1. The new object must be derived from pdfObj
2. The new object must have a copy constructor

The final code appears inside the class definition thusly:

template <typename T> void addItem(const T *newItem)
 {
     pdfObj *item = new T(*newItem);
     mArray->push_back(item);
 }


这篇关于如何实例化正确的对象类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆