C++ 指向引用的指针getter
我有一个带有引用的类,需要一个返回指针的getterC++ 指向引用的指针getter,c++,class,pointers,reference,getter,C++,Class,Pointers,Reference,Getter,我有一个带有引用的类,需要一个返回指针的getter class X { std::string& text; public: auto GetText() -> decltype(text) * { return &text); // doesn't work X(std::string& text): text(text) {} }; 最简单的方法是向这个类传递一个指针。但是如果我传递一个引用,我能得到一个带有getter的指针吗 编辑
class X {
std::string& text;
public:
auto GetText() -> decltype(text) * { return &text); // doesn't work
X(std::string& text): text(text) {}
};
最简单的方法是向这个类传递一个指针。但是如果我传递一个引用,我能得到一个带有getter的指针吗
编辑:以下是错误消息
error: cannot declare pointer to 'std::__cxx11::string& {aka class std::__cxx11::basic_string<char>&}'
auto GetText() -> decltype(text) * { return &text);
^
错误:无法声明指向“std::u cxx11::string&{aka class std::uu cxx11::basic_string&}”的指针
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和*<代码>是错误的。调用来解决这个问题,例如
auto GetText() -> std::remove_reference_t<decltype(text)> * { return &text);
auto GetText()->std::remove_reference_t*{return&text);
但是,在这种情况下,auto
无论如何都会正确地推断出您的类型,因此您的显式声明是不必要的。我已经为我的初始问题做了一个解释。该程序有一个带指针的类和一个返回引用的getter,第二个带引用的类和一个返回指针的getter
class X {
std::string& text;
public:
auto GetText() -> decltype(text) * { return &text); // doesn't work
X(std::string& text): text(text) {}
};
而且似乎->std::remove\u reference\t
可以被->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;
}
//g++main.cpp-o test\u reference\u pointer&&strip-s test\u reference\u pointer&./test\u reference\u pointer
#包括
//具有指针和返回引用的getter的类。
甲级{
std::字符串*文本;
公众:
std::string&GetText_old_way(){return*text;}
auto GetText\u pure\u auto(){return*text;}
自动获取文本\u指针\u算术()->decltype(*text)和{return*text;}
公众:
A(std::string*text):text(text){}
};
//具有引用和返回指针的getter的类。
B类{
std::字符串和文本;
公众:
std::string*GetText\u old\u way(){return&text;}
auto GetText_pure_auto(){return&text;}
自动获取文本\u指针\u算术()->decltype(&text){return&text;}
auto GetText_remove_reference()->std::remove_reference_t*{return&text;}
公众:
B(std::string&text):text(text){}
};
int main(){
std::string text=“你好,世界”;
{//TEST
A(&text);
无符号整数i{0};
std::cout“不起作用”不是一个有用的问题描述。你有编译错误吗?如果有,请把它们放在你的问题中。为什么不直接做std::string*GetText(){return&text;}
?你的代码也有打字错误(当然你的意思是}
,而不是)
关闭该功能。)`所有这些似乎都完全没有必要。这就是当你使用闲置的水冷器时所发生的“总是自动”的情况按照字面意思运行,不需要任何反射。如果可以,但为什么要这样做?正如Kerrek所说,所有这些似乎都是完全不必要的。非常有趣!感谢大家。注意在类A
,GetText\u pure\u auto
中返回的是底层字符串的副本,而不是引用,这可能不是一个好的idea.在类A
中,在auto-GetText\u-pointer\u算术中,decltype(*text)&
的尾部&
是不必要的;decltype(*text)
是等价的。此外,如果使用C++14,这个签名可以写得更简洁,如decltype(*text)GetText\u-pointer\u算术()
或decltype(auto)
或auto&
“而且似乎->std::remove\u reference\t可以替换为->decltype(&text)”您有点错。std::remove\u reference\t*
可以替换为decltype(&text)
在这种情况下,不是std::remove_reference_t
@ampcartney为什么auto GetText_pointer_算术()->decltype(*text){return*text;}
与auto GetText_pointer_算术()->decltype(*text)&{return text*text;}相同
?第一个看起来像是返回一个std::string
,而不是std::string&
。您知道当取消引用指针时,结果类型是引用,对吗?