assignment overloading using pointer

I am writing an assignment using operator overloading. The assignment using reference works fine. I also try to use pointer, however it does not seems to use the function. See code below:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "stdafx.h"
#include <stdio.h>

class Base
{
private:
	int a;
public:
	int p;
	Base(){};
	~Base(){};
	Base &operator=( const Base &src )
	{ 
		printf( "assign using reference\n"); 
		if ( &src != this )
		{
			a = src.a;
			p = src.p;
		}
		return *this;
	}
	Base &operator=( const Base *src )
	{ 
		printf("assign using pointer\n"); 
		if ( src != this )
		{
			a = src->a;
			p = src->p;
		}
		return *this;

	}
};


 
int _tmain(int argc, _TCHAR* argv[])
{
	Base b1;
	Base b2;
	Base *pb1;
	Base *pb2;

	pb1 = new Base();
	pb2 = new Base();
	b2 = b1;
	pb2 = pb1;
	return 0;
}


The output is:
assign using reference

I was hoping it will use the following function for the pointer assignment:
Base &operator=( const Base *src )
The compile is using default assignment when doing assignment: pb2 = pb1 instead of my assignment. So what is happing here? Any comments on this will be appreciated.

pb2 is a pointer, pb1 is a pointer, operators only work with objects so you would need to dereference pb2.
*pb2 = pb1;
Good point, thanks a lot.
Topic archived. No new replies allowed.