how to campare name of data member(not their value), sending as a argument from function. 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.