zoukankan      html  css  js  c++  java
  • 类名

    简单,用类名实例化一个对象,即可作为参数进行传递!
    定义一个类
    class A;
    实例化类对象
    A a;

    定义 函数
    int f(A x){};

    函数调用
    void main()
    {
    f(a);//a为参数
    }
    一个函数(包括普通函数和成员函数)可以被多个类声明为“朋友”,可以引用多个类中的私有数据. 
    一个函数相当于一个接口,跟所定义的类无关,类中中只是使用它而已,如果不是main函数中定义的函数所有地方都可以使用,给它定义为友元只不过是为了这个函数能使用当前类的私有成员
    
    

    //=====================================
    // EX0807.cpp
    // 友元改成普通函数
    //=====================================
    class Animal{
    int itsWeight;
    int itsAge;
    public:
    void setWeight(int w){ itsWeight=w; }
    void setAge(int a){ itsAge=a; }
    };//-----------------------------------
    void setValue(Animal& ta, int tw, int tn){
    ta.setWeight(tw);
    ta.setAge(tn);
    }//------------------------------------
    int main(){
    Animal peppy;
    setValue(peppy, 7, 9);
    }//====================================

    //----------------------

    //=====================================
    // EX0807ff.cpp
    // 友元改成成员函数
    //=====================================
    class Animal{
    int itsWeight;
    int itsAge;
    public:
    void setValue(int w, int a);
    };//-----------------------------------
    void Animal::setValue(int tw, int tn){
    itsWeight = tw;
    itsAge = tn;
    }//------------------------------------
    int main(){
    Animal peppy;
    peppy.setValue(7, 9);
    }//====================================

     
  • 相关阅读:
    我叫mt3.0更新公告
    gcc 编译器常用的命令行参数一览
    C++两个类相互引用错误留影
    C++中的声明与定义
    C++ 杂记
    C++中两个类相互包含引用的相关问题
    Ogre 中使用OIS的两种模式
    Ogre 渲染队列(二)
    Ogre 渲染队列(一)
    Ogre 场景管理器
  • 原文地址:https://www.cnblogs.com/herizai/p/3081652.html
Copyright © 2011-2022 走看看