简体   繁体   English

C ++中的构造函数行为

[英]constructor behaviour in C++

How is this possible?? 这怎么可能??
test t2=50 ??? test t2=50 ??? as t2 is object a.....so how can it be equal ti an integer using visual studio 2008 由于t2是对象a .....所以如何使用Visual Studio 2008将其等于一个整数

class test
{
public :
 int a,b;
 test(int x=0,int y=0)
 {
  a=x;
  b=y;
 }
};


void g()
{
 test t1=test(10,20);
 test t2=50;
 cout<<t1.a<<":"<<t1.b<<endl;
 cout<<t2.a<<":"<<t2.b<<endl;
}

int main()
{
 g();
 system("pause");
}

Since your constructor is not defined as explicit the compiler uses the constructor defined in test class to create a test object by passing the integer argument to the constructor (Parameter x ). 由于未将构造函数定义为explicit因此编译器使用在test类中定义的构造函数通过将整数参数传递给构造函数(参数x )来创建test对象。 To avoid this declare the constructor as explicit . 为避免这种情况,请将构造函数声明为explicit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM