holy now
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
class deffunc
{
private:
struct item1
{
int a;
};
struct item2 {
struct item1 a;
};
public:
/*
typedef int * pintmy;
const pintmy ci; // equal int *const ci;
const int * cj;
*/
deffunc():b(2) {a = 1; e = (struct item2 *)malloc(sizeof(struct item2)); e->a.a = 5;};
~deffunc();
deffunc(const deffunc &obj);
deffunc &operator =(const deffunc &obj);
deffunc *operator &();
const deffunc *operator &() const;
/*
it is a mistake like this:
deffunc *operator &() const;
*/
/* operator new reload */
void *operator new(size_t size, void *addr);
void *operator new(size_t size, char *msg);
void *operator new[](size_t size, char *msg);
/* operator ++ reload */
deffunc &operator ++() {a++; return *this;} // ++deffuncobj
deffunc operator ++(int){deffunc tmp(*this); ++(*this); return tmp;} //deffuncobj--
struct item1 &operator *() const {return e->a;}
/* then written like this:
int &operator *() const {return test.a;}
is a big mistake:"const int can not be converted to int &" */
struct item1 *operator ->() const {return &(e->a);}
bool operator ==(const deffunc &obj) const {return a == obj.a;}
deffunc& operator +=(const deffunc &obj)
{
a += obj.a;
return *this;
}
deffunc operator +(const deffunc &obj)
{
deffunc tmp = *this;
return tmp += obj;
}
bool operator ()(const deffunc &obj) {return a > obj.a;}
void setmember(int val) {a = val;}
void print() {printf("a=%d, b=%d, c=%d, d=%d\n", a, b, c ,d);}
private:
int a;
const int b;
static int c;
static const int d = 4;
struct item2 *e;
struct item2 test;
};
int deffunc::c = 3;
deffunc::~deffunc() {}
deffunc::deffunc(const deffunc &obj): b(obj.b)
{
a = obj.a;
e = (struct item2 *)malloc(sizeof(struct item2));
e->a.a = obj.e->a.a;
}
deffunc &deffunc::operator =(const deffunc &obj)
{
this->a = obj.a;
return *this;
}
deffunc *deffunc::operator &()
{
return this;
}
const deffunc *deffunc::operator &() const
{
return this;
}
void *deffunc::operator new(size_t size, void *addr)
{
return addr;
}
void *deffunc::operator new(size_t size, char *msg)
{
printf("%s\n", msg);
return ::operator new(size);
}
void *deffunc::operator new[](size_t size, char *msg)
{
printf("%s\n", msg);
return ::operator new(size);
}
int main()
{
deffunc obj;
obj.setmember(100);
obj.print();
deffunc obj1;
obj1 = obj;
obj1.print();
printf("-1-------------1-\n\n");
void *addr = malloc(sizeof(deffunc));
deffunc *pobj = new(addr) deffunc;
printf("pobj: %x, addr: %x\n", pobj, addr);
pobj->print();
free(addr);
printf("-2-------------2-\n\n");
deffunc *pobj1 = new("reload operator new") deffunc;
pobj1->print();
deffunc *pobj2 = new("reload operator new []") deffunc[20];
pobj2[19].print();
printf("-3-------------3-\n\n");
obj.print();
deffunc obj2 = ++obj;
obj2.print();
deffunc obj3 = obj2++;
obj3.print();
printf("obj3: %d\n", (*obj3).a);
printf("obj3: %d\n", obj3->a);
printf("-4-------------4-\n\n");
obj2.print();
obj3.print();
printf("operator (): %d\n", obj2(obj3));
printf("all will be OK\n");
}