从继承的对象检索信息
retrieving information from inherited objects
我正在尝试实现从简单到复杂的继承对象的层次结构,以使对象具有尽可能多的面向对象功能的方式进行,但我填补了这项工作可以在许多方面得到改进。非常欢迎任何建议。特别是我不知道如何使用继承功能来解决以下问题的安全方法:
- 对象 Atom 包含从更简单的对象 Contraction 继承的对象 BasisFunction 向量。但是在 Atom 中,我需要访问每个 BasisFunction 的向量 zeta 和 C 中包含的信息。如何修改它以使其成为可能?
- 层次结构中的所有对象都是可复制的吗?如何引入面向对象的功能?
- 最后,我想有一个分子单例。如何定义它?
- 我关注的是这种实施方法的表现。哪里可以改进?
此时,我将显示代码:
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
class Contraction{
protected:
vector<double> zeta;
vector<double> c;
vec A;
public:
Contraction(){} /*contructor*/
Contraction(vector<double> Zeta,vector<double> C, vec a):zeta(Zeta),c(C),A(a){}
/*contructor*/
~Contraction(){} /*destructor*/
bool deepcopy(const Contraction& rhs) {
bool bResult = false;
if(&rhs != this) {
this->zeta=rhs.zeta;
this->c=rhs.c;
this->A=rhs.A;
bResult = true;
}
return bResult;
}
public:
Contraction(const Contraction& rhs) { deepcopy(rhs); }
Contraction& operator=(const Contraction& rhs) { deepcopy(rhs); return *this; }
};
class BasisFunction: public Contraction{
protected:
vector<int> n;
vector<int> l;
vector<int> m;
bool deepcopy(const BasisFunction& rhs) {
bool bResult = false;
if(&rhs != this) {
this->zeta=rhs.zeta;
this->c=rhs.c;
this->A=rhs.A;
this->n=rhs.n;
this->l=rhs.l;
this->m=rhs.m;
bResult = true;
}
return bResult;
}
public:
BasisFunction(){};/*How to define this constructor to initialize the inherited elements too?*/
~BasisFunction(){};
};
class Atom{
protected:
int Z;
vec R; ///Position
vec P; ///Momentum
vec F; ///Force
double mass;
vector<BasisFunction> basis;
public:
/*Here I need to define a function that uses the information in vectors c_i and zeta_i of the vector basis, how could it be achieved?*/
};
vector<Atom> Molecule; /*I nedd transform this in a singleton, how?*/
提前谢谢。
- 如何修改它以使其成为可能?您应该公开
Contraction
受保护的字段,或者Contraction
Getter/Setter 方法以访问此文件,或者重新设计类的所有层次结构以将此字段恰好放在需要它们的位置。 - 层次结构中的所有对象都是可复制的吗?如何引入面向对象的功能?类中没有私有复制构造函数/赋值运算符,它们没有指向其中数据的原始指针,因此可以安全地复制它们。
- 最后,我想有一个分子单例。怎么可能 定义?您不能从向量分子接收单例,因为这是对象的包含器而不是对象本身。这里不需要单例,因为你实际上只能在范围内有一个"分子"命名向量。但是,如果你想让signleton保存分子数据,你应该添加一个类shell:
class Molecule
{
public:
static Molecule & Instance() { static Molecule instance; return instance; }
const vector<Atom> & GetMyData() const { return data; }
vector<Atom> & GetMyData() { return data; }
private:
Molecule() {};
Molecule(const Molecule & rhs);
const Molecule & operator=(const Molecule & rhs);
vector<Atom> data;
};
这是迈耶斯的经典单例实现。
- 我担心这种方法对 实现。哪里可以改进?为了提高性能,您需要删除处理虚拟函数调用和间接寻址的每个算法步骤中的所有内容。在你的代码中,这意味着你真的不应该在你的基类中实现get/set方法,因为现在它们是逻辑结构 - 只是有序的数据持有者。如果您经常查看 zeta、c 和其他字段,您必须可以直接访问那里。
相关文章:
- 如何在 Linux 中务实地检索连接的接入点信息
- Qt 错误:QSqlQuery::value:尝试从表中检索统计信息时未定位在有效记录上 (QComboBox)
- 使用 GetNetworkParams() 检索网络信息时出错
- 32位到64位:LNK2019错误,如何检索相关信息
- C MACOS-编程检索代码符号证书信息
- 从类模块检索信息以在 switch 语句中输出时出现问题
- 使用友元函数从多态类中检索类型信息
- 如何在C++中以编程方式检索 64 位进程的详细信息
- 从网页中检索特定信息
- 正在检索有关当前登录会话的信息(Visual C++,Win 7)
- 如何检索连接到服务器的硬盘信息?示例 C/C 代码
- 正在检索系统和操作系统信息
- 从 CPtrArray MFC 输入和检索信息
- 检索数据结构对齐信息
- 从继承的对象检索信息
- 如何从该数组中检索信息
- 从部分google protobuf消息中检索信息
- c++如何从具有多个参数的节点中检索信息
- 更改时区的FILETIME信息检索
- Qt 5.5和OpenGL:检索设备信息