Стандартные типы C++, такие как int или char, имеют ctors, поэтому вы можете иметь такие выражения, как:
int a = int(67); // create anonymous variable and assing it to variable a
int b(13); // initialize variable b
int(77); // create anonymous variable
Пользовательские типы (структуры или классы) могут делать то же самое:
struct STRUCT
{
STRUCT(int a){}
};
STRUCT c = STRUCT(67);
STRUCT d(13);
STRUCT(77);
Вопрос: почему мы можем передать ссылочную анонимную структуру или экземпляр класса, но не можем передать стандартные типы?
struct STRUCT
{
STRUCT(int a){}
};
void func1(int& i){}
void func2(STRUCT& s){}
void func3(int i){}
void func4(STRUCT s){}
void main()
{
//func1(int(56)); // ERROR: C2664
func2(STRUCT(65)); // OK: anonymous object is created then assigned to a reference
func3(int(46)); // OK: anonymous int is created then assigned to a parameter
func4(STRUCT(12)); // OK: anonymous object is created then assigned to a parameter
}
func2
не должна компилироваться (по той же причине, что и первая). - person Mat   schedule 09.11.2013void main()
нестандартен. - person n. 1.8e9-where's-my-share m.   schedule 09.11.2013warning C4239: nonstandard extension used : 'argument' : conversion from 'STRUCT' to 'STRUCT &'; A non-const reference may only be bound to an lvalue
. Вы должны использовать/W4
. - person n. 1.8e9-where's-my-share m.   schedule 09.11.2013void main
неправильно. - person Konrad Rudolph   schedule 09.11.2013it shall have a return type of type int
. Здесь не так много свободы. - person n. 1.8e9-where's-my-share m.   schedule 10.11.2013