Program
7
#include
#include
static void mynewhandler();
static char* reservememory;
void initnewhandler()
{
reservememory = new char[100000];
#include
static void mynewhandler();
static char* reservememory;
void initnewhandler()
{
reservememory = new char[100000];
std::set_new_handler(&myNewHandler);
}
static void mynewhandler()
{
delete [] reservememory;
}
static void mynewhandler()
{
delete [] reservememory;
std::cerr << "out of memory (use emerengy memory)"
<< std::endl;
throw std::bad_alloc();
}
<< std::endl;
throw std::bad_alloc();
}
Program 8
#include
#include
static void mynewhandler();
static char* reservememory1;
static char* reservememory2;
void initnewhandler()
{
reservememory1 = new char[1000000];
reservememory2 = new char[100000];
std::set_new_handler(&mynewhandler);
}
static void mynewhandler()
{
static bool firstKiss = true;
if (firstKiss)
static void mynewhandler();
static char* reservememory1;
static char* reservememory2;
void initnewhandler()
{
reservememory1 = new char[1000000];
reservememory2 = new char[100000];
std::set_new_handler(&mynewhandler);
}
static void mynewhandler()
{
static bool firstKiss = true;
if (firstKiss)
{
firstKiss = false;
delete [] reserveMemory1;
std::cerr << "Warning: almost out of memory" << std::endl;
}
else
firstKiss = false;
delete [] reserveMemory1;
std::cerr << "Warning: almost out of memory" << std::endl;
}
else
{
delete [] reserveMemory2;
std::cerr << "out of memory" << std::endl;
throw std::bad_alloc();
}
}
delete [] reserveMemory2;
std::cerr << "out of memory" << std::endl;
throw std::bad_alloc();
}
}
Program 9
#include
#include
#include
void* myclass::operator new (std::size_t size)
{
std::cout << "call myclass::new" << std::endl;
return ::new char[size];
}
void* myclass::operator new[] (std::size_t size)
{
std::cout << "call myclass::new[]" << std::endl;
#include
void* myclass::operator new (std::size_t size)
{
std::cout << "call myclass::new" << std::endl;
return ::new char[size];
}
void* myclass::operator new[] (std::size_t size)
{
std::cout << "call myclass::new[]" << std::endl;
return ::new char[size];
}
}
Program
10
#include
#include
class myclass
class myclass
{
public:
void func1()
public:
void func1()
{
std::cout << "call of func1()" << std::endl;
}
void func2()
std::cout << "call of func1()" << std::endl;
}
void func2()
{
std::cout << "call of func2()" << std::endl;
}
};
typedef void (myclass::*myclassfunction) ();
int main()
{
myclassfunction funcptr;
myclass x;
funcptr = & myclass::func1;
(x.*funcptr)();
std::cout << "call of func2()" << std::endl;
}
};
typedef void (myclass::*myclassfunction) ();
int main()
{
myclassfunction funcptr;
myclass x;
funcptr = & myclass::func1;
(x.*funcptr)();
funcptr = & myclass::func2;
(x.*funcPtr)();
}
}
No comments:
Post a Comment