[英]how to compare name of data member of class not their value
how to campare name of data member(not their value), sending as a argument from function. 如何将数据成员的名称(不是它们的值)作为campare,从函数中作为参数发送。 The example code is as follows.
示例代码如下。
class Example
{
private:
std::string value;
public:
void Set(const std:: string& MemberName)
{
if(MemeberName == value)
{
std::cout<<"Same Member Name";
}
else
{
std::cout<<"Not same Member name";
}
}
...
}
i did this way but not getting the required result. 我这样做但没有得到所需的结果。 Thansks for the help
谢谢你的帮助
It should be 它应该是
if(MemeberName == "value")
{
std::cout<<"Same Member Name";
}
else
{
std::cout<<"Not same Member name";
}
In case you want to find out whether it is the exactly same object, you might use the address, where it is stored: 如果您想知道它是否是完全相同的对象,您可以使用存储它的地址:
#include <iostream>
using namespace std;
std::string g;
void foo(const std::string& arg)
{
if (&arg == &g) // compares addresses
std::cout << "same object" << std::endl;
if (arg == g) // compares values
std::cout << "same value" << std::endl;
}
int main() {
g = "abc";
foo(g);
return 0;
}
Note that when you pass by reference, you are actually working with the same object (no copy is being created) and thus when you use &
operator you can retrieve the address of original object. 请注意,当您通过引用传递时,实际上您正在使用相同的对象(没有创建副本),因此当您使用
&
运算符时,您可以检索原始对象的地址。 This example prints both same object
and same value
. 此示例打印
same object
和same value
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.