S
sarathy
Hi all,
I was wondering how the program below was working. I
overloaded the new operator, which is responsible for allocating memory
for the object. But I made an explicit memory allocation [ zero bytes
]. When the object was created, somehow the value was assigned
correctly and it prints the correct result. How is this possible ???
# include <iostream>
using namespace std;
class A
{
long a;
public:
A(long x) { a=x; }
long getA(){return this->a;}
void* operator new (size_t);
void operator delete (void *);
};
void* A:perator new (size_t size)
{
void *ptr = malloc(0);
return ptr;
}
void A:perator delete (void *p)
{
free(p);
}
int main()
{
A *a=new A(1234567);
cout << a->getA() << endl;
delete a;
return 0;
}
I was wondering how the program below was working. I
overloaded the new operator, which is responsible for allocating memory
for the object. But I made an explicit memory allocation [ zero bytes
]. When the object was created, somehow the value was assigned
correctly and it prints the correct result. How is this possible ???
# include <iostream>
using namespace std;
class A
{
long a;
public:
A(long x) { a=x; }
long getA(){return this->a;}
void* operator new (size_t);
void operator delete (void *);
};
void* A:perator new (size_t size)
{
void *ptr = malloc(0);
return ptr;
}
void A:perator delete (void *p)
{
free(p);
}
int main()
{
A *a=new A(1234567);
cout << a->getA() << endl;
delete a;
return 0;
}