gpt4 book ai didi

c++ - 返回一个Type,或者如何保存一个对象指针的类型?

转载 作者:太空狗 更新时间:2023-10-29 20:26:06 27 4
gpt4 key购买 nike

我有一个非常复杂的代码结构,但重要的部分是:

典型设置:我有一个基类和两个从该基类派生的类,每个类都有自己的成员,并且没有标准构造函数

class BaseSolver{
...
};

class SolverA : BaseSolver{
public:
std::string a;
SolverA(TypeA objectA);
};

class SolverB : BaseSolver{
public:
int b;
SolverB(TypeB objectB);
};

现在我有一个配置 xml 文件,我从中读取是否必须使用 SolverASolverB。因此我有一个 IOService:

template<class T>
class IOService
{
BaseSolver* getSolver()
{
std::string variableThatIReadFromXML;

/* here I have to perform many actions before I can create a solver object
* to retrieve the data needed for the constructors */

TypeA variableIConstrucedWithDataFromXML;
TypeB anotherVariableIConstrucedWithDataFromXML;

if (variableThatIReadFromXML == "a")
return new SolverA(variableIConstrucedWithDataFromXML); // I know that this can leak memory
else if (variableThatIReadFromXML == "b")
return new SolverB(anotherVariableIConstrucedWithDataFromXML);
}
};

在我的应用程序中的某处(为简单起见,假设它是 main.cpp):

int main(){
IOService ioService;
BaseSolver* mySolver = ioService.getSolver();
}

那绝对没问题。

但是现在,主要我必须分别访问派生类ab 的成员。我该怎么做?

我想只从 IOService 中检索 Solver 的类型:

class IOService
{
decltype getSolverType()
{
std::string variableThatIReadFromXML;

/* here I have to perform many actions before I can create a solver object
* to retrieve the data needed for the constructors */

TypeA variableIConstrucedWithDataFromXML;
TypeB anotherVariableIConstrucedWithDataFromXML;

if (variableThatIReadFromXML == "a")
return new SolverA(variableIConstrucedWithDataFromXML); // I know that this can leak memory
else if (variableThatIReadFromXML == "b")
return new SolverB(anotherVariableIConstrucedWithDataFromXML);
}

TypeA getConstructorDataForSolverA()
{
/* here I have to perform many actions before I can create a solver object
* to retrieve the data needed for the constructors */

return variableIConstrucedWithDataFromXML;
}


TypeB getConstructorDataForSolverB()
{
/* here I have to perform many actions before I can create a solver object
* to retrieve the data needed for the constructors */

return anotherVariableIConstrucedWithDataFromXML;
}
};

当然我不能指定decltype作为返回值。

我真的很无奈。我将不胜感激任何关于正确方向的提示,甚至是解决此问题的方法。

[编辑]:派生的求解器类需要的不仅仅是来自 xml 文件的信息才能正常工作。这意味着,我必须设置更多来自网格文件的属性。所以我可以将网格文件提供给 IOService,以便 IOService 可以这样设置适当的成员:

class IOService
{
BaseSolver* getSolver(MeshType myMesh)
{
std::string variableThatIReadFromXML;

/* here I have to perform many actions before I can create a solver object
* to retrieve the data needed for the constructors */

TypeA variableIConstrucedWithDataFromXML;
TypeB anotherVariableIConstrucedWithDataFromXML;

if (variableThatIReadFromXML == "a")
{
auto solverA = new SolverA(variableIConstrucedWithDataFromXML); // I know that this can leak memory
solverA.a = mesh.a;
}
else if (variableThatIReadFromXML == "b")
{
auto solverB = new SolverB(anotherVariableIConstrucedWithDataFromXML);
solverB.b = mesh.b;
}
}
};

但是 IOService 需要知道类 MeshType,这是我想避免的,因为我认为它会破坏封装。所以我想在我的程序的另一部分分别设置成员ab(这里为了简单起见在main中)。

考虑到这一点,只有 Daniel Daranas 的回答对我来说似乎是一个解决方案。但我想避免动态转换。

因此,重新表述的问题可能是:我应该如何更改我的设计以确保封装并避免动态转换? [/Edit]

我正在使用 clang 3.4 ob ubuntu 12.04 lts。

最佳答案

使用dynamic_cast 尝试将指向基类的指针转换为指向派生类的指针。如果基类指向的对象不存在(基指针的NULL值),或者实际上不是派生类对象,则返回NULL。相反,如果结果不为 NULL,则您有一个有效的指向派生类的指针。

int main(){
IOService ioService;
BaseSolver* mySolver = ioService.getSolver();
SolverB* bSolver = dynamic_cast<SolverB*>(mySolver);
if (bSolver != NULL)
{
int finallyIGotB = bSolver->b;
cout << finallyIGotB;
}
}

请注意,可能有一些比使用 dynamic_cast 更好的设计解决方案。但至少这是一种可能性。

关于c++ - 返回一个Type,或者如何保存一个对象指针的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21313596/

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