1、三目运算符
C语言中的三目运算符:
#include"stdio.h"
void main()
{
int a = 10;
int b = 15;
int z = (a > b ? a : b);
printf("z:%d", z);
(a > b ? a : b) = 20; //error:错误 1 error C2106: “=”: 左操作数必须为左值
printf("z:%d", z);
}
修改:
#include"stdio.h"
void main()
{
int a = 10;
int b = 15;
int z = (a > b ? a : b);
printf("z:%d\n", z);
*(a > b ? &a : &b) = 20; //error:错误 1 error C2106: “=”: 左操作数必须为左值
printf("z:%d\n", *(a > b ? &a : &b));
}
C++中的三目运算符:
#include<iostream>
using namespace std;
void main()
{
int a = 10;
int b = 15;
int z = (a > b ? a : b);
cout << z << endl;
(a > b ? a : b) = 20; //成功
cout << (a > b ? a : b) << endl;
}
结论:在C语言中,三目运算符的结果不能当左值,表达式返回的是一个值。而在C++中,三目运算符返回的不是一个值,而是一个变量的本身,也就是说返回的是一个变量的内存首地址。
2、const——http://blog.csdn.net/songshimvp1/article/details/48163495
3、define和const
void DEFINE()
{
#define a 10
cout << a << endl;
}
void DEFINE2()
{
cout << a << endl; //成功
}
void CONST()
{
const int b = 15;
cout << b << endl;
}
void CONST2()
{
cout << b << endl; //错误1 error C2065 : “b” : 未声明的标识符
}
另外,#define缺乏类型检测机制!
小提示:不要在#define后面手残的加“;”,否则会出现“错误1 error C2143: 语法错误 : 缺少“;”(在“<<”的前面) ”。
4、new和delete
new:不光分配内存,而且会调用对象的构造函数;
delete:不光释放内存,而且会调用对象的析构函数;