我将加载多个 XML 文件,每个文件都放入 aQDomDocument
中,然后使用 aQMap
将标识字符串与每个文档相关联。我应该在地图中存储 aQDomDocument
还是指向 a 的指针QDomDocument
?即,以下哪个示例更符合 Qt 最佳设计实践。
我怀疑示例 A 是首选。我看到的所有代码示例都只是QDomDocument
在堆栈上创建一个本地。并且,sizeof( QDomDocument )
是4个字节;因此,QDomDocument
它可能是一个薄包装器,可以在不影响性能的情况下进行浅层复制。
A:地图包含QDomDocument
实例
class Core
{
private:
QMap<QString, QDomDocument> docs;
public:
Core( void )
{
QFile file( "alpha.xml" );
file.open( QIODevice::ReadOnly );
QDomDocument doc;
doc.setContent( &file );
docs["alpha"] = doc;
// ... etc for other XML files
}
QString findThing( QString const & docName, QString const & thingName )
{
QDomDocument doc = docs[docName];
// ... search the doc for the thing with the given name
}
};
B. Map 包含指向QDomDocument
实例的指针
class Core
{
private:
QMap<QString, QDomDocument *> docs;
public:
Core( void )
{
QFile file( "alpha.xml" );
file.open( QIODevice::ReadOnly );
QDomDocument * pDoc = new QDomDocument();
pDoc->setContent( &file );
docs["alpha"] = pDoc;
// ... etc for other XML files
}
QString findThing( QString const & docName, QString const & thingName )
{
QDomDocument * pDoc = docs[docName];
// ... search the doc for the thing with the given name
}
};