void main()
{
int *ip = new int(10);
complex *cp = new complex;
float * fp = new float[10];
delete [] fp;
delete cp;
delete ip;
}
4 byte(s) allocated!
8 byte(s) allocated!
40 byte(s) allocated!
memory block returned!
memory block returned!
memory block returned!
在这例子中,malloc()与free()被重新拾起,替代了new delete的功能。同时,new () delete()函数声明为static类型,以防止它们的重载对其它文件产生副作用。在未重载new、delete之前,系统会使用缺省的那一份new delete版本。
操作符重载是一张最令你自豪的Ace,但必须记住它仍具有以下限制:①操作符重载要求操作对象至少有一个是类对象(类只是结构的一个广义概念)。我曾经做过以下的尝试: //error: 'operator+(char*char*)’ must he a member function or have a parameter of class type
char*operator+(char*s1 char* s2)
{
return strcat(sl s2);
}
void main()
{
int *ip = new int(10);
complex *cp = new complex;
float * fp = new float[10];
delete [] fp;
delete cp;
delete ip;
}
4 byte(s) allocated!
8 byte(s) allocated!
40 byte(s) allocated!
memory block returned!
memory block returned!
memory block returned!
在这例子中,malloc()与free()被重新拾起,替代了new delete的功能。同时,new () delete()函数声明为static类型,以防止它们的重载对其它文件产生副作用。在未重载new、delete之前,系统会使用缺省的那一份new delete版本。
操作符重载是一张最令你自豪的Ace,但必须记住它仍具有以下限制:①操作符重载要求操作对象至少有一个是类对象(类只是结构的一个广义概念)。我曾经做过以下的尝试: //error: 'operator+(char*char*)’ must he a member function or have a parameter of class type
char*operator+(char*s1 char* s2)
{
return strcat(sl s2);
}
但后来编译器证明了这种对基本数据类型的多情是愚蠢的。
②不可以构造新操作符,也不能改变操作符操作参数的数目,不能改变操作符的优先级。
③操作符的含义应尽量忠实于操作符的原义,这不是一条严格的规则,但是一条很好的忠告。譬如,当你将complex的‘!’操作定义成机器重新启动的代码,虽然C++没有理由阻拦你,但这样不好。
void main()
{
int *ip = new int(10);
complex *cp = new complex;
float * fp = new float[10];
delete [] fp;
delete cp;
delete ip;
}
4 byte(s) allocated!
8 byte(s) allocated!
40 byte(s) allocated!
memory block returned!
memory block returned!
memory block returned!
在这例子中,malloc()与free()被重新拾起,替代了new delete的功能。同时,new () delete()函数声明为static类型,以防止它们的重载对其它文件产生副作用。在未重载new、delete之前,系统会使用缺省的那一份new delete版本。
操作符重载是一张最令你自豪的Ace,但必须记住它仍具有以下限制:①操作符重载要求操作对象至少有一个是类对象(类只是结构的一个广义概念)。我曾经做过以下的尝试: //error: 'operator+(char*char*)’ must he a member function or have a parameter of class type
char*operator+(char*s1 char* s2)
{
return strcat(sl s2);
}
void main()
{
int *ip = new int(10);
complex *cp = new complex;
float * fp = new float[10];
delete [] fp;
delete cp;
delete ip;
}
4 byte(s) allocated!
8 byte(s) allocated!
40 byte(s) allocated!
memory block returned!
memory block returned!
memory block returned!
在这例子中,malloc()与free()被重新拾起,替代了new delete的功能。同时,new () delete()函数声明为static类型,以防止它们的重载对其它文件产生副作用。在未重载new、delete之前,系统会使用缺省的那一份new delete版本。
操作符重载是一张最令你自豪的Ace,但必须记住它仍具有以下限制:①操作符重载要求操作对象至少有一个是类对象(类只是结构的一个广义概念)。我曾经做过以下的尝试: //error: 'operator+(char*char*)’ must he a member function or have a parameter of class type
char*operator+(char*s1 char* s2)
{
return strcat(sl s2);
}
但后来编译器证明了这种对基本数据类型的多情是愚蠢的。
②不可以构造新操作符,也不能改变操作符操作参数的数目,不能改变操作符的优先级。
③操作符的含义应尽量忠实于操作符的原义,这不是一条严格的规则,但是一条很好的忠告。譬如,当你将complex的‘!’操作定义成机器重新启动的代码,虽然C++没有理由阻拦你,但这样不好。