指向引用的指针 getter

Pointer getter to a reference

我有一个带参考的 class 并且想要一个 getter 那个 returns 一个指针。

class X {
    std::string& text;
public:
    auto GetText() -> decltype(text) * { return &text); // doesn't work
    X(std::string& text): text(text) {}
};

最简单的方法是传递一个指向此 class 的指针。但是如果我传递一个引用,我能得到一个带getter的指针吗?

编辑:这里是错误信息

error: cannot declare pointer to 'std::__cxx11::string& {aka class std::__cxx11::basic_string<char>&}'
auto GetText() -> decltype(text) * { return &text);
                                 ^

首先,

auto GetText() -> decltype(text) * { return &text); // doesn't work

是一种非常可恶的声明此签名的方式。喜欢

std::string* GetText(){ return &text);

甚至只是

auto GetText(){ return &text);

但这不是代码审查。

这里的问题是您要求一个指向 text 成员变量的声明类型的指针,它是一个字符串引用 (std::string&)。从评论部分来看,您似乎没有意识到 decltype 尊重其参数的“引用”性、“常量”性和“易变性”。

在 C++ 中不能有指向引用的指针,例如std::string&* 格式错误。调用 std::remove_reference_t 应该可以解决该问题,例如

auto GetText() -> std::remove_reference_t<decltype(text)> * { return &text);

但是,在这种情况下,auto 无论如何都会正确地推断出您的类型,因此您的显式声明是不必要的。

我已经为我最初的问题做了一个 test program。该程序有一个带指针的 class 和一个 returns 引用的 getter 和第二个带引用的 class 和一个 returns 的 getter ] 一个指针。

而且 -> std::remove_reference_t<decltype(text)> 似乎可以用 -> decltype(&text) 代替。

随时发表评论。

// g++ main.cpp -o test_reference_pointer && strip -s test_reference_pointer && ./test_reference_pointer

#include <iostream>

// A class with a pointer and a getter that returns a reference.
class A {
    std::string *text;
public:
    std::string& GetText_old_way() { return *text; }
    auto GetText_pure_auto() { return *text; }
    auto GetText_pointer_arithmetic() -> decltype(*text) & { return *text; }
public:
    A(std::string *text): text(text) {}
};

// A class with a reference and a getter that returns a pointer.
class B {
    std::string& text;
public:
    std::string *GetText_old_way() { return &text; }
    auto GetText_pure_auto() { return &text; }
    auto GetText_pointer_arithmetic() -> decltype(&text) { return &text; }
    auto GetText_remove_reference() -> std::remove_reference_t<decltype(text)> * { return &text; }
public:
    B(std::string& text): text(text) {}
};

int main() {
    std::string text = "hello, world";

    {//TEST
        A a(&text);
        unsigned int i{0};

        std::cout << "-- Test 1:"<< std::endl;
        ++i; std::cout << i << ". " << a.GetText_old_way() << std::endl;
        ++i; std::cout << i << ". " << a.GetText_pointer_arithmetic() << std::endl;
        ++i; std::cout << i << ". " << a.GetText_pure_auto() << std::endl;
        std::cout << std::endl;
    }

    {//TEST
        B b(text);
        unsigned int i{0};

        std::cout << "-- Test 2:"<< std::endl;
        ++i; std::cout << i << ". " << *b.GetText_old_way() << std::endl;
        ++i; std::cout << i << ". " << *b.GetText_pointer_arithmetic() << std::endl;
        ++i; std::cout << i << ". " << *b.GetText_remove_reference() << std::endl;
        ++i; std::cout << i << ". " << *b.GetText_pure_auto() << std::endl;
        std::cout << std::endl;
    }

    return 0;
}