/**
* ~~~~~ One argument constructor with operator overloading ~~~~~
*
* Demonstrates which is called when, when both overloaded assignment operator
* and one-argument constructor is defined.
*
* Observation: During object creation, one argument constructor is called, while during
* object assignment, operator function is called.
*/
#include <iostream>
using namespace std;
class A{
private :
static int a;
public :
A( int ) ;
void print( ) ;
static void operator = ( int ) ;
} ;
// Allocation for static
A:: a ;
// One argument constructor
A:: A ( int b) {
cout << "Inside one argument constructor" << endl;
this- > a= b;
}
static void A:: operator = ( int b) {
cout << "Inside operator function" << endl;
this- > a = b;
}
void A:: print ( ) {
cout << "Value of a =" << a<< endl;
}
int main( ) {
/* INITIALIZATION */
A obj1= 2 ;
obj1.print ( ) ;
/* ASSIGNMENT */
obj1= 3 ;
obj1.print ( ) ;
return 0 ;
}
LyoqCiAqICAgfn5+fn4gT25lIGFyZ3VtZW50IGNvbnN0cnVjdG9yIHdpdGggb3BlcmF0b3Igb3ZlcmxvYWRpbmcgfn5+fn4KICogCiAqIERlbW9uc3RyYXRlcyB3aGljaCBpcyBjYWxsZWQgd2hlbiwgd2hlbiBib3RoIG92ZXJsb2FkZWQgYXNzaWdubWVudCBvcGVyYXRvcgogKiBhbmQgb25lLWFyZ3VtZW50IGNvbnN0cnVjdG9yIGlzIGRlZmluZWQuCiAqIAogKiBPYnNlcnZhdGlvbjogRHVyaW5nIG9iamVjdCBjcmVhdGlvbiwgb25lIGFyZ3VtZW50IGNvbnN0cnVjdG9yIGlzIGNhbGxlZCwgd2hpbGUgZHVyaW5nCiAqIG9iamVjdCBhc3NpZ25tZW50LCBvcGVyYXRvciBmdW5jdGlvbiBpcyBjYWxsZWQuCiAqLwoKI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgQXsKCnByaXZhdGU6CglzdGF0aWMgaW50IGE7CnB1YmxpYzoKCUEoaW50KTsKCXZvaWQgcHJpbnQoKTsKCXN0YXRpYyB2b2lkIG9wZXJhdG9yID0oaW50KTsKfTsKCi8vIEFsbG9jYXRpb24gZm9yIHN0YXRpYwpBOjphOwoKLy8gT25lIGFyZ3VtZW50IGNvbnN0cnVjdG9yCkE6OkEoaW50IGIpewoJY291dDw8Ikluc2lkZSBvbmUgYXJndW1lbnQgY29uc3RydWN0b3IiPDxlbmRsOwoJdGhpcy0+YT1iOwp9CgpzdGF0aWMgdm9pZCBBOjogb3BlcmF0b3IgPShpbnQgYil7Cgljb3V0PDwiSW5zaWRlIG9wZXJhdG9yIGZ1bmN0aW9uIjw8ZW5kbDsKCXRoaXMtPmEgPSBiOwp9Cgp2b2lkIEE6OnByaW50KCl7Cgljb3V0PDwiVmFsdWUgb2YgYSA9Ijw8YTw8ZW5kbDsKfQoKaW50IG1haW4oKSB7CgkKCS8qIElOSVRJQUxJWkFUSU9OICovCglBIG9iajE9MjsKCW9iajEucHJpbnQoKTsKCQoJLyogQVNTSUdOTUVOVCAqLwoJb2JqMT0zOwoJb2JqMS5wcmludCgpOwoJCglyZXR1cm4gMDsKfQ==
compilation info
prog.cpp:21:28: error: ‘static void A::operator=(int)’ must be a nonstatic member function
static void operator =(int);
^
prog.cpp:17:13: error: ‘int A::a’ is private
static int a;
^
prog.cpp:25:4: error: within this context
A::a;
^
prog.cpp:25:1: error: ‘a’ in ‘class A’ does not name a type
A::a;
^
prog.cpp:33:13: error: prototype for ‘void A::operator=(int)’ does not match any in class ‘A’
static void A:: operator =(int b){
^
prog.cpp:14:7: error: candidates are: A& A::operator=(A&&)
class A{
^
prog.cpp:14:7: error: A& A::operator=(const A&)
stdout