operator=

Pages: 12
i'm a little bit confused about this kinda function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class test {
	public:
	test operator= (const test &t) {
		a = t.a;
		b = t.b;
		//my question is, "who" is the pointer? 
		return *this;
	}

	private:
	int a, b;
};

int main () {
	test a, b;
	//if there's operation like this
	a = b;
	cout << a;
	getch();
	return 0;
}

this points to a for this particular call.
It would be weird if it were b, wouldn't it?
a = b; could be written as a.operator= ( b );
"this" is a predefined pointer which points to private members of the class and the program you wrote make no sense ........
Rameez wrote:
"this" is a predefined pointer which points to private members of the class and the program you wrote make no sense ........

That's not correct. The this pointer points to the object itself, not to its members. You can use it to refer to a member (this->a), but it's not necessary (you could just say a). And the program does make sense, although those members could use some initialization.
if you're going to use cout on a 'test' class object, you'll also need to overload operator<<, have you done that?
I just made an experiment that hopefully clarifies things:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
class test {
public:
  static int globalCounter;
//You are supposed to run this test twice. 
//Once with the below line turned on, and once with the below line commented out.
  test(const test& t) { this->globalCounter++; };

  test operator= (const test &t) {
    this->a = t.a;
    this->b = t.b;
    return *this;
  };
  test(){this->globalCounter++;};
private:
  int a, b;
};

int test::globalCounter=0;
int main()
{ test a;
  test b;
  a.operator=(b);
  std::cout << "Number of test objects created: " << test::globalCounter << std::endl;
  return 0;
}


On gcc compiler:
1) the code as it is will produce answer 3.
2) the code with the indicated line commented out will produce answer 2.


[EDIT]:

hohoohoohohoho try this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;


class test {
public:
  static int globalCounter;
  test(const test& t)
  { this->globalCounter++;
    std::cout << this->globalCounter << " ";
    this->operator=(t);
  };
  test operator= (const test &t) {
    a = t.a;
    b = t.b;
    return *this;
  };
  test(){this->globalCounter++;};
private:
  int a, b;
};

int test::globalCounter=0;
int main()
{ test a;
  test b;
  a.operator=(b);
  std::cout << test::globalCounter << std::endl;
  return 0;
}


Last edited on
1
2
//test test::operator= (const test &t)
test & test::operator=(const test &t);
The same for your second example and you avoid the infinite loop/stack overflow
Last edited on
@filipe: thanks dude for your help,
and when we write ("return *this"). Does it return the object of the (class)test type.
Rameez wrote:
and when we write ("return *this"). Does it return the object of the (class)test type.

Exactly.
Which is why you should do what ne555 suggested and return a reference instead of a copy.
this points to a for this particular call.
It would be weird if it were b, wouldn't it?


but i don't understand, a is not a pointer. if you said that:
1
2
3
4
5
6
7
8
9
10
void somefunction (test &b)
if (this == &b)
cout << "yes!";
}

void main () {
test *a, b;
a = &b;
a->somefunction(b);
}


i can understand that, but in this case, a is not a pointer.


if you're going to use cout on a 'test' class object, you'll also need to overload operator<<, have you done that?

really? i don't know about that, but why? isn't it you can choose what operator to use?
That's what is happening in the "test operator= (const test &t)" function. You can do the same with the << and in the implementation you can individually cout each data field. That way you can just cout an instance of a class easily in your program.
isn't it what quircky saying is if you made a operator overloading than that means that you have to made a cout operator too (operator<<) ?
1
2
3
4
test operator= (const test &t) {
  // ...
  return *this;    // This is a recursive call!
}


Should be:
1
2
3
4
test& operator= (const test &t) {
  // ...
  return *this;
}

Last edited on
1
2
3
4
test& operator= (const test &t) {
  // ...
  return *this;
}


did you mean that this function returns the value of variable a and return the address?
i can understand that, but in this case, a is not a pointer.

You're mixing up objects and pointers to objects. this always points to the object a member function has been called with.

With
1
2
3
test *a, b;
a = &b;
a->somefunction(b);

a points to b and in somefunction, this also points to b.
b is an actual test object, whereas a just points to a test object. When you dereference a, you get a reference to b.

With this:
1
2
test a, b;
a = b;

operator= is called on a, so "this" points to a inside operator=.
// This is a recursive call!

It may cause a recursive call.

The result of the function is copy-constructed.

IIRC.
@athar: ok i understand that this doesn't always refer to a pointer but the caller of the function or class method

@duoas: right, it return the value from the caller to the caller
it's me again... :D i have another question, what is referenced by this:

&operator<<

is it the caller (left side of the operator)?
Pages: 12