1。符合类型(引用,指针)//数组是否属于
的两种不同形式的定义:
int a = 10;
定义一个指针:
int *p = &a;
int (*pp) = &a;
定义一个引用:
int &ra = a;
int (&rra) = a;
2。而且变量定义语句的识别优先于函数调用
class Test
{
public:
Test(string* p){cout<<"ok"<<endl;}
Test(int i){cout << "okokok" << endl;}
};
int main()
{
string a;
//Test(&a); // 想创建一个匿名对象,为什么编译不过?(vs2005)
//Test(&b);
Test(3);
Test(&string()); // 显式ok
int i = 10;
int *p = &i;
cout << *p << endl;
int (*pp) = &i;
cout << *pp << endl;
return 0;
}
Test(&a)编译器优先认为它是一个引用变量的声明,等价于Test &a;
这明显错,因为引用必需初始化,而且这个引用变量与string对象a重名了
Test(&b)这个则仅仅因为是认为是引用,定义需的初始化
Test(3)这个不存在和变量定义语句冲突的情形,所以创建了临时对象,可见,创建临时对象用这样的方式是正确的,错的只是那个构造函数的使用形式与变量定义语句相冲突了
据说vs2005 Test(&string())不能显式“ok”,个人认为编译器的优化行为,这个临时对象未被使用,所以编译器未构造它
的两种不同形式的定义:
int a = 10;
定义一个指针:
int *p = &a;
int (*pp) = &a;
定义一个引用:
int &ra = a;
int (&rra) = a;
2。而且变量定义语句的识别优先于函数调用
class Test
{
public:
Test(string* p){cout<<"ok"<<endl;}
Test(int i){cout << "okokok" << endl;}
};
int main()
{
string a;
//Test(&a); // 想创建一个匿名对象,为什么编译不过?(vs2005)
//Test(&b);
Test(3);
Test(&string()); // 显式ok
int i = 10;
int *p = &i;
cout << *p << endl;
int (*pp) = &i;
cout << *pp << endl;
return 0;
}
Test(&a)编译器优先认为它是一个引用变量的声明,等价于Test &a;
这明显错,因为引用必需初始化,而且这个引用变量与string对象a重名了
Test(&b)这个则仅仅因为是认为是引用,定义需的初始化
Test(3)这个不存在和变量定义语句冲突的情形,所以创建了临时对象,可见,创建临时对象用这样的方式是正确的,错的只是那个构造函数的使用形式与变量定义语句相冲突了
据说vs2005 Test(&string())不能显式“ok”,个人认为编译器的优化行为,这个临时对象未被使用,所以编译器未构造它