Created
June 17, 2011 08:20
-
-
Save andreabalducci/1031064 to your computer and use it in GitHub Desktop.
Testing assignment operator override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OperatorTest.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <conio.h> | |
class Base | |
{ | |
public: | |
int _value; | |
public: | |
Base() | |
{ | |
_value = 0; | |
} | |
virtual void operator=(const Base& source) | |
{ | |
_value = source._value*2; | |
} | |
virtual void Dump() | |
{ | |
printf("Base _value = %3d \n", _value); | |
} | |
}; | |
class Enhanced : public Base | |
{ | |
public: | |
int _anotherValue; | |
public: | |
Enhanced() { | |
_anotherValue = 0; | |
} | |
virtual void Dump(){ | |
printf("Enhanced _value = %3d anotherValue = %3d \n", _value, _anotherValue); | |
} | |
}; | |
class Enhanced2 : public Base | |
{ | |
public: | |
int _anotherValue; | |
public: | |
Enhanced2() { | |
_anotherValue = 0; | |
} | |
virtual void operator=(const Enhanced2& source) | |
{ | |
__super::operator=(source); | |
} | |
virtual void Dump(){ | |
printf("Enhanced2 _value = %3d anotherValue = %3d \n", _value, _anotherValue); | |
} | |
}; | |
class Enhanced3 : public Base | |
{ | |
private: | |
int _anotherValue; | |
public: | |
Enhanced3() { | |
_anotherValue = 0; | |
} | |
void SetAnotherValue(int v) { | |
_anotherValue = v; | |
} | |
int GetAnotherValue() const{ | |
return _anotherValue; | |
} | |
virtual void Dump(){ | |
printf("Enhanced3 _value = %3d anotherValue = %3d \n", _value, _anotherValue); | |
} | |
}; | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
// OK | |
{ | |
printf("Base assignment operator: "); | |
Base a; a._value = 10; | |
Base b; | |
b = a; | |
b.Dump(); | |
} | |
// FAIL :: c++ add a default copy operator | |
{ | |
printf("Default assignment operator: "); | |
Enhanced a; a._value = 10; a._anotherValue = 100; | |
Enhanced b; | |
b = a; | |
b.Dump(); | |
} | |
// OK :: override default copy operator | |
{ | |
printf("Overrided assignent: "); | |
Enhanced2 a; a._value = 10; a._anotherValue = 100; | |
Enhanced2 b; | |
b = a; | |
b.Dump(); | |
} | |
// FAIL :: c++ add a default copy operator | |
{ | |
printf("Private member copy: "); | |
Enhanced3 a; a._value = 10; a.SetAnotherValue(100); | |
Enhanced3 b; | |
b = a; | |
b.Dump(); | |
} | |
// Pointer assignment :: ok | |
{ | |
printf("Enhanced pointer assignment: "); | |
Enhanced a; a._value = 10; a._anotherValue = 100; | |
Enhanced b; | |
Enhanced *pa = &a, *pb = &b; | |
*pb = *pa; | |
b.Dump(); | |
} | |
// Pointer assignment :: Fail | |
{ | |
printf("Base pointer assignment: "); | |
Enhanced a; a._value = 10; a._anotherValue = 100; | |
Enhanced b; | |
Base *pa = &a, *pb = &b; | |
*pb = *pa; | |
b.Dump(); | |
} | |
_getch(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Base assignment operator: Base _value = 20 | |
Default assignment operator: Enhanced _value = 20 anotherValue = 100 | |
Overrided assignent: Enhanced2 _value = 20 anotherValue = 0 | |
Private member copy: Enhanced3 _value = 20 anotherValue = 100 | |
Enhanced pointer assignment: Enhanced _value = 20 anotherValue = 100 | |
Base pointer assignment: Enhanced _value = 20 anotherValue = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment