优雅地访问嵌套类中的数据

Accessing data from a nested class elegantly

本文关键字:数据 嵌套 访问      更新时间:2023-10-16

我有以下类:

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;
};

无关紧要,但我建议简单地使用单独的ToStringHexToStringDecToStringBin功能。节省不存储引用,而且 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