zoukankan      html  css  js  c++  java
  • 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。 也就是说:当存在多种解释的时候,编译器会优先认为某种解释,而这种解释可能不是你所期望的。

  • 相关阅读:
    自定义组件
    vue 父子组件传值数据不能实时更新问题
    vuex(2)
    vuex(1)
    mysql-忘记密码
    转发&重定向
    mysql主从配置
    mysql安装脚本
    1、JAVA数据类型
    maven 国内阿里云镜像配置
  • 原文地址:https://www.cnblogs.com/nzbbody/p/4621793.html
Copyright © 2011-2022 走看看