C++ 对象的定义

1、考虑下面的方法
void Print(const Student& s)
{
printf("Student[%s:%d] ",
s._Name.c_str(),
s._Age);
}
2、方法Print接收一个Student对象,定义Student对象,并调用方法,有哪些方式?
方式一:
Student s;
Print(s);
方式二:
Student s = Student();
Print(s);
方式三:
Print(Student()); // 匿名对象
方式四:
Student* s = new Student();
Print(*s);
方式五:
Student* s = new Student;
Print(*s);
注意:不能使用下面的方式,
Student s();
Print(s);
报错 “Print”: 不能将参数 1 从“Student (__cdecl *)(void)”转换为“const Student &”
原因是:编译器把Student s(); 当成一种方法声明,返回Student,接收void。 也就是说:当存在多种解释的时候,编译器会优先认为某种解释,而这种解释可能不是你所期望的。

原文地址:https://www.cnblogs.com/nzbbody/p/4621793.html