Question about setting string

Under the setName string function. Why is it that if I put test = name instead of name = test It would show nothing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <string>

using namespace std;

class TestClass{
    public:
    string setName (string test)
    {
        name = test;
        return name;
    }

    private:
    string name;
};
int main(){

    TestClass Object;
    cout << Object.setName("test");
    return 0;

}
Note that = is not an equality operator like you're used to from math. In C++ = is the assignment operator. It assigns the value on the right to the variable on the left.

 
name = test; // Assigns the value of test to the variable name. 
Last edited on
Oh thank you so much. So I was passing the value into test and which left the name variable with no value in it then it got assigned to test thus returning nothing.
Topic archived. No new replies allowed.