【问题标题】:retrieving information from inherited objects从继承的对象中检索信息
【发布时间】:2014-12-17 09:37:47
【问题描述】:

我正在尝试实现从简单到复杂的继承对象的层次结构,这样做的方式是使对象具有尽可能多的面向对象的功能,但我认为这项工作可以在很多方面得到改进。任何建议都非常受欢迎。特别是我不知道如何使用继承功能来安全地解决以下问题:

  1. 对象 Atom 包含一个对象向量 BasisFunction,该向量继承自更简单的对象 Contraction。但在 Atom 中,我需要访问每个 BasisFunction 的向量 zeta 和 C 中包含的信息。如何修改它以使其成为可能?
  2. 层次结构中的所有对象都可复制吗?如何引入面向对象的功能?
  3. 最后我想要一个分子单例。如何定义?
  4. 我关心这种实施方法的性能。哪里可以改进?

此时我将展示代码:

#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?*/

提前致谢。

【问题讨论】:

标签: c++ oop inheritance operator-overloading


【解决方案1】:
  1. 如何修改它以使其成为可能?您应该公开Contraction 的受保护字段或在Contraction 中创建Getter/Setter 方法来访问这些文件,或者重新设计类的所有层次结构以使这些字段准确地存在于需要它们的位置。
  2. 层次结构中的所有对象都可复制吗?如何引入面向对象的特性? 您的类中没有私有复制构造函数/赋值运算符,并且它们没有指向数据的原始指针,因此可以安全地复制它们。
  3. 最后我想要一个分子单例。怎么可能 定义? 您不能从矢量分子接收单例,因为这是对象的容器而不是对象本身。这里不需要单例,因为在范围内实际上只能有一个名为“分子”的向量。但是,如果您希望 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;
};
这是 Meyers 的经典单例实现。
  1. 我担心这种方法的性能 执行。哪里可以改进? 为了提高性能,您需要在处理虚拟函数调用和间接的每个算法步骤中删除所有内容。在您的代码中,这意味着您真的不应该在基类中实现 get/set 方法,因为现在它们在逻辑上是结构 - 只是有序的数据持有者。如果您经常查看 zeta、c 和其他字段,您必须可以直接访问那里。

【讨论】:

  • 在 C++11 中,你可以使用 =delete 作为复制构造函数
  • 是的,但我没有看到关于主题启动问题的 C++11 徽章。
  • 将非常量引用返回到static 变量的原因是什么?
  • 您有时可能想更改数据。所以 Molecule::Instance().GetMyData() 将适合这个。如果你有 static const Molecule & Instance() 这是不可能的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
相关资源
最近更新 更多