优雅地从嵌套类访问数据

时间:2019-10-11 10:01:40

标签: c++ class oop c++11

我有以下课程:

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()一样简单的解决方案是什么?

2 个答案:

答案 0 :(得分:2)

您必须以某种方式为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;
};

Irelevant,但我建议您简单地使用单独的ToStringHexToStringDecToStringBin函数。节省了不存储引用的时间,而且API通过这种方式更容易。

答案 1 :(得分:1)

我在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