优雅地访问嵌套类中的数据
Accessing data from a nested class elegantly
我有以下类:
class BigNum
{
public:
BigNum(string StrNumber) : Number(std::move(StrNumber)) {}
BigNum(const char *StrNumber) : Number(string(StrNumber)) {}
~BigNum() = default;
struct
{
string HEX() { return Number + " - HEX"; }
string DEC() { return Number + " - DEC"; }
string BIN() { return Number + " - BIN"; }
}ToString;
private:
string Number;
};
最后,我以下列方式优雅地从该结构访问函数:
BigNum a = "1234";
cout << "a = " << a.ToString.DEC() << endl;
cout << "b = " << a.ToString.HEX() << endl;
这里的问题是我无法从我的结构访问变量Number
。
我知道这样的事情可以解决我的问题:
struct
{
string HEX(BigNum &parent) { return parent.Number + " - HEX"; }
...
}ToString;
此解决方案的问题在于,始终将指针传递给我的实例是不舒服的。
在这种情况下,将数据放在嵌套类中并同时保持调用像a.ToString.DEC()
一样简单,解决方案是什么?
在某种程度上,您必须为ToString
提供指向BigNum
对象的引用或指针,以便您可以访问Number
。像这样的事情怎么样:
class BigNum
{
public:
BigNum(string StrNumber) : Number(std::move(StrNumber)) {}
BigNum(const char* StrNumber) : Number(string(StrNumber)) {}
~BigNum() = default;
// you can make the struct private so the type is not visible externally
struct ToStringType
{
private:
const BigNum& ref;
public:
ToStringType(const BigNum& r) : ref(r) {}
string HEX() { return ref.Number + " - HEX"; }
string DEC() { return ref.Number + " - DEC"; }
string BIN() { return ref.Number + " - BIN"; }
};
ToStringType ToString{ *this };
private:
string Number;
};
无关紧要,但我建议简单地使用单独的ToStringHex
、ToStringDec
和ToStringBin
功能。节省不存储引用,而且 API 通过这种方式更容易。
我在 ToString 结构中看不到任何理由。
只需将方法留在 BIGNUM 中即可完成。
但是,对于这个特定的应用程序(在ostream
中更改给定对象的渲染样式(,我会让你的对象以典型的operator<<
重载打印,然后使用 io-manipulators 修改渲染风格,以便您将能够:
cout << "a (DEC) = " << BigNum::DEC << a << endl;
cout << "a (HEX) = " << BigNum::HEX << a << endl;
一个完整的示例:
#include <iostream>
#include <iomanip>
using namespace std;
class BigNum
{
public:
BigNum(string StrNumber) : Number(std::move(StrNumber)) {}
BigNum(const char *StrNumber) : Number(string(StrNumber)) {}
~BigNum() = default;
static std::ios_base& DEC(std::ios_base& os) {
os.iword(rendering_style_xalloc) = 0;
return os;
}
static std::ios_base& HEX(std::ios_base& os) {
os.iword(rendering_style_xalloc) = 1;
return os;
}
static std::ios_base& BIN(std::ios_base& os) {
os.iword(rendering_style_xalloc) = 2;
return os;
}
private:
static int rendering_style_xalloc;
string Number;
friend ostream &operator << (ostream &ostr, const BigNum &bignum);
};
int BigNum::rendering_style_xalloc = std::ios_base::xalloc();
ostream &operator << (ostream &os, const BigNum &bignum) {
switch (os.iword(BigNum::rendering_style_xalloc)) {
case 0:
os << bignum.Number << " - DEC";
break;
case 1:
os << bignum.Number << " - HEX";
break;
case 2:
os << bignum.Number << " - BIN";
break;
default:
os << bignum.Number << " - UNK";
break;
}
return os;
}
int main(int argc, char **argv)
{
BigNum a = "1234";
cout << BigNum::DEC << "a (DEC) = " << a << endl;
cout << BigNum::HEX << "a (HEX) = " << a << endl;
}
引用: https://en.cppreference.com/w/cpp/io/ios_base/iword
相关文章:
- 嵌套在类中时无法设置成员数据
- 嵌套结构,从由空行分隔的文件中读取数据
- 如何访问嵌套类的数据?
- 优雅地访问嵌套类中的数据
- 嵌套的 STL 数据结构
- 当使用嵌套类功能时,使用非静态数据成员的使用无效,但是当函数未固定时可以
- 使用MPI/OpenMP的C 程序带有派生数据类型(嵌套类对象)容器
- 访问嵌套结构的数据成员并将结构传递到功能C
- C 嵌套的数据结构利用结构
- 如何在 C++ 中将结构类型向量数据输入到结构向量成员(嵌套结构向量)中
- 为什么链表中的数据在嵌套函数中会更改/损坏?
- 嵌套的C 常数数据结构,没有初始化列表或明确命名所有节点-VC 2012过早破坏了内部元素
- 访问嵌套结构中的数据
- C /STL公共迭代器,用于深嵌套的私人数据
- 从数据成员(一次性嵌套类)中获取非 POD 对象的地址
- 在C++中,如何让(嵌套)比较函子引用封闭类的数据
- HDF5中嵌套的复合数据类型
- 用大量唯一数据填充 c++ 中的嵌套结构
- 链表类,如何访问嵌套节点结构的数据
- 结构化数据:嵌套三角形(使用结构并进行一些几何计算)