对引用的理解

#include 
#include 
float temp; //全局变量
float fn1(float r)
{
    temp = r * r * 3.14;
    return temp;
}
float& fn2(float r) 
{
    temp = r * r * 3.14;
    return temp;
}

void main()
{


    float a = fn1(5.0);  //1
    //float& b = fn1(5.0);  //2,对动态内存,已经销毁的内存进行引用
    float c = fn2(5.0);  //3
    float& d = fn2(5.0); //4
    std::cout << "a:" << a << std::endl;        //可行,容器A储存的值
    std::cout << "c:" << c << std::endl;        //可行,容器C储存的引用值
    std::cout << "d:" << d << std::endl;        //对动态内存建立的引用,但VS非必要不销毁,暂时没事
}

你可能感兴趣的:(c++,visual,studio,开发语言)