1、考虑下面的方法
void Print(const Student& s)
{
printf("Student[%s:%d]\n",
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。 也就是说:当存在多种解释的时候,编译器会优先认为某种解释,而这种解释可能不是你所期望的。

相关文章:

  • 2021-11-13
  • 2021-07-17
  • 2022-12-23
  • 2021-10-10
  • 2021-05-29
  • 2021-10-26
  • 2021-09-26
  • 2022-12-23
猜你喜欢
  • 2021-12-09
  • 2022-12-23
  • 2021-06-02
  • 2022-02-11
  • 2021-05-17
  • 2022-12-23
相关资源
相似解决方案