C++ 派生内部类的成员
目标 我正在致力于实现一个C++ 派生内部类的成员,c++,inheritance,C++,Inheritance,目标 我正在致力于实现一个整数,它是抽象代数中的一种结构。这类环是加法下的阿贝尔群(我已经实现了)。吊环配有两个操纵器,+和* 实施选择 出于这个原因,我决定将IntegerGroup定义为具有GroupElements且具有运算符的类。完整的工作代码如下所示: 整群 IntegerRing.cpp 编译器认为IntegerRing::Element的复制构造函数实际上是一个返回int的函数 错误截图 以下是错误的屏幕截图: 如何解决所有这些问题?原因是您无法访问类的私有字段。 继承/嵌套类不会
整数
,它是抽象代数中的一种结构。这类环是加法下的阿贝尔群(我已经实现了)。吊环配有两个操纵器,+和*
实施选择
出于这个原因,我决定将IntegerGroup
定义为具有GroupElement
s且具有运算符的类。完整的工作代码如下所示:
整群
IntegerRing.cpp
编译器认为IntegerRing::Element
的复制构造函数实际上是一个返回int
的函数
错误截图
以下是错误的屏幕截图:
如何解决所有这些问题?原因是您无法访问类的私有字段。 继承/嵌套类不会改变这一点。(例外是内部类始终可以访问其封闭类的任何成员(自C++11以来))
对于日志
中使用IntegerGroup::GroupElement的第一个错误
应该是usingIntegerGroup::GroupElement::GroupElement代码>代码>整数::元素< /代码>,顺便说一下,我不知道这个类的需要。证明我多年来没有用C++来处理严重的OOP,这使我忘记了事情。首先:派生类可以访问受保护的
,公共
成员,而不是私有
,除非您在基类中声明派生类a朋友
第二:如何编写复制构造函数。不幸的是,派生类可以访问自己继承的受保护的数据成员,而不是基类。为了解决这个问题,我只编写了如下复制构造函数:
IntegerRing::Element::Element(const IntegerGroup::GroupElement::GroupElement& el)
: IntegerGroup::GroupElement(el)
{
}
对于依赖项,我的代码是最小的。你是说构造函数的IntegerRing::Element::Element
,而不仅仅是IntegerRing::Element
?这就是我的意思。抢手货编辑:在IntegerGroup::GroupElement
中,只需要一个构造函数。我刚刚删除了它。请不要发布文本截图。发布文本本身。那么,我应该如何解决这个问题呢?是否将getter和setter添加到基嵌套类?(注意:我学校的服务器,就是我运行这一切的环境,没有C++11)或者你可以尝试使用friend
关键字?举个例子,IntegerGroup
根本不需要包含数组。我只是有闪回:我可以在基类中使用protected
,几年前,我记得上一次我在C++中做了一些严肃的面向对象编程时,遇到了这个问题,并通过使用受保护的数据成员代替私有成员来修复它。
#include "IntegerGroup.h"
#include <new>
#include <iostream>
IntegerGroup::IntegerGroup()
{
}
IntegerGroup::IntegerGroup(int n)
: n(n), elements(Array<IntegerGroup::GroupElement>(n))
{
//this is to have integers in [0,n-1]
for (int j = 0; j < n; j++)
{
this->createNewElement(j);
}
}
void IntegerGroup::createNewElement(int m)
{
// create new GroupElement
GroupElement newElement(m, this);
// store it at index m in elements
this->elements[m] = newElement;
}
IntegerGroup::GroupElement::GroupElement()
: group(0)
{
}
IntegerGroup::GroupElement::GroupElement(int m, IntegerGroup * g)
: group(g)
{
// this->m must be in [0, g->size() - 1]
this->m = m % g->size();
if (this->m < 0) this->m = g->size() + this->m;
}
IntegerGroup::GroupElement::~GroupElement()
{
if (this->group)
{
this->group = 0;
}
}
IntegerGroup::GroupElement IntegerGroup::identity() const
{
// IntegerGroup consists of all integers in [0, n-1], and identity is 0
return this->elements[0];
}
// this group is simply the integers mod n, and should be populated integers in [0,n-1]
// thus, multiplication is simply a matter of returning the element at index (a+b)%n
IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*(const IntegerGroup::GroupElement& b)
{
// if the group is not defined
if (!this->group)
// we simply perform integer multiplication
return GroupElement(this->val() * b.val());
// otherwise, perform group multiplication
return GroupElement((this->val() + b.val()) % this->group->size());
}
IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*=(const IntegerGroup::GroupElement& b)
{
return ((*this) = (*this) * b);
}
bool IntegerGroup::GroupElement::operator==(const IntegerGroup::GroupElement& b)
{
return this->m == b.m;
}
bool IntegerGroup::GroupElement::operator!=(const IntegerGroup::GroupElement& b)
{
return !(*this == b);
}
int IntegerGroup::GroupElement::val() const { return this->m; }
int IntegerGroup::size() const { return this->n; }
#ifndef INTEGERRING_H
#define INTEGERRING_H
#include "IntegerGroup.h"
#include "Operators.h"
class IntegerRing : public IntegerGroup
{
public:
class Element : public IntegerGroup::GroupElement
{
public:
using IntegerGroup::GroupElement;
/*Element();
Element(int);
Element(int, IntegerRing*);
~Element();*/
operator IntegerGroup::GroupElement() { return IntegerGroup::GroupElement(); }
Element(const IntegerGroup::GroupElement& el)
{
// copy everything from el into *this
this->m = el.m;
this->group = el.group;
}
/*Element operator+(const Element&);
Element operator-(const Element&);
Element operator*(const Element&);
Element operator+=(const Element&);
Element operator-=(const Element&);
Element operator*=(const Element&);*/
};
Element identity(Operators);
private:
};
#endif
#include "IntegerRing.h"
#include "IntegerGroup.h"
#include "Operators.h"
/*IntegerRing::Element::Element()
{
}*/
/*IntegerRing::Element(const IntegerGroup::GroupElement& el)
{
// copy everything from el into *this
this->m = el.m;
this->group = el.group;
}
/*
IntegerRing::Element IntegerRing::Element::operator+(const IntegerRing::Element& b)
{
// IntegerRing is simply Abelian group under addition
// thus, we treat the elements like group elements first, multiply under that group, and cast to ring elements
return (IntegerRing::Element)(((IntegerGroup::GroupElement)(*this)) * ((IntegerGroup::GroupElement)b));
}
IntegerRing::Element IntegerRing::Element::operator-(const IntegerRing::Element& b)
{
int val;
// if this has a group
if (this->group)
{
// compute (this->m - b.m) % this->group->size()
val = (this->m - b.m) % this->group->size();
// if that value is negative, add this->group->size() to it
if (val < 0) val = this->group->size() + val;
}
// otherwise, val is simply the integer difference of this->m,b.m
else val = this->m - b.m;
// return element with this value
return Element(val);
}
IntegerRing::Element IntegerRing::Element::operator*(const IntegerRing::Element& b)
{
if (this->group)
return IntegerRing::Element((this->m - b.m) % this->group->size());
return IntegerRing::Element(this->m - b.m);
}
IntegerRing::Element IntegerRing::Element::operator+=(const IntegerRing::Element& b)
{
return ((*this) = (*this) + b);
}
IntegerRing::Element IntegerRing::Element::operator-=(const IntegerRing::Element& b)
{
return ((*this) = (*this) - b);
}
IntegerRing::Element IntegerRing::Element::operator*=(const IntegerRing::Element& b)
{
return ((*this) = (*this) * b);
}
*/
IntegerRing::Element IntegerRing::identity(Operators op)
{
// if op is ADDITIVE
if (op == ADDITIVE)
// return what the base version of this method would return
return (IntegerRing::Element)(((IntegerGroup::GroupElement*)this)->identity());
// multiplicative identity requested, and it is 1
return (IntegerRing::Element)this->elements[0];
}
#ifndef OPERATORS_H
#define OPERATORS_H
enum Operators
{
ADDITIVE, MULTIPLICATIVE
};
#endif
IntegerRing::Element::Element(const IntegerGroup::GroupElement::GroupElement& el)
: IntegerGroup::GroupElement(el)
{
}