zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c++临时对象的来源

C++对象 临时 来源
2023-06-13 09:14:43 时间

首先看下面一端代码:

复制代码代码如下:

 #include<iostream>
 voidswap(int&a,int&b)
 {
    inttemp;
    temp=a;
    a=b;
    b=temp;
 }

 intmain(intargc,char**argv)
 {
    inta=1,b=2;
    swap(a,b);
    std::cout<<a<<"-----"<<b<<std::endl;
    return0;
 }

 结果为
 2-----1  
 可能大多数园友,认为"inttemp"是"临时对象",但是其实不然,"inttemp"仅仅是swap函数的局部变量。

临时对象是代码中看不到的,但是实际程序中确实存在的对象。临时对象是可以被编译器感知的。


为什么研究临时对象?
主要是为了提高程序的性能以及效率,因为临时对象的构造与析构对系统开销也是不小的,所以我们应该去了解它们,知道它们如何造成,从而尽可能去避免它们。

临时对象建立一个没有命名的非堆对象会产生临时对象。(不了解什么是堆对象和非堆对象,可以参考C++你最好不要做的这一博文,这里面有介绍。)这种未命名的对象通常在三种条件下产生:为了使函数成功调用而进行隐式类型转换时候、传递函数参数和函数返回对象时候。

那么首先看看为了使函数成功调用而进行隐式类型转换。

复制代码代码如下:

 #include<iostream>
 intcountChar(conststd::string&s,constcharc)
 {
     intcount=0;
     for(inti=0;i<s.length();i++)
     {
         if(*(s.c_str()+i)==c)
         {
             count++;
        }
   }
    returncount;
 }

 intmain(intargc,char**argv)
 {
    charbuffer[200];
    charc;
    std::cout<<"pleaseinputthestring:";
    std::cin>>buffer;
    std::cout<<"pleaseinputthecharwhichyouwanttochount:";
    std::cin>>c;
    intcount=countChar(buffer,c);
    std::count<<"thecountis:"<<count<<std::endl;
    return0;
 }

结果为:



这里调用函数countChar(conststd::string&s,constchar&c),那么我们看看这个函数的形参是conststd::string&s,形参类型为conststd::string,但是实际上传递的是charbuffer[200]这个数组。其实这里编译器为了使函数调用成功做了类型转换,char*类型转换为了std::string类型,这个转换是通过一个赋值构造函数进行的,以buffer做为参数构建一个std::string类型的临时对象。当constChar返回时,即函数撤销,那么这个std::string临时对象也就释放了。但是其实从整个程序上来说临时对象的构造与释放是不必要的开销,我们可以提高代码的效率修改一下代码避免无所谓的转换。所以知道临时对象的来源,可以对程序性能上有一个小小提升。

  注意仅当通过传值方式传递对象或者传递常量引用参数,才会发生这类型的转换,当传递非常量引用的参数对象就不会发生。因为传递非常量的引用参数的意图就是想通过函数来改变其传递参数的值,但是函数其实是改变的类型转换建立的临时对象,所以意图无法实现,编译器干脆就直接拒绝。

第二种情况是大家熟悉的函数传递参数的时候,会构造对应的临时对象。看下面一段代码运行的结果想必就一清二楚了。

复制代码代码如下:
 #include<iostream>
 classPeople
 {
    public:
        People(std::stringn,inta)
        :name(n),age(a)
        {
            std::count<<"h2"<<std::endl;
        }
       People()
       {
           std::count<<"h1"<<std::endl;
       }
       People(constPeople&P)
       {
           name=p.name;
           age=p.age;
           std::cout<<"h3"<<std::endl;
       }
       std::stringname;
       intage;
};

voidswap(Peoplep1,Peoplep2)
{
   Peopletemp;
   temp.age=p1.age;
   temp.name=p1.name;
   p1.age=p2.age;
   p1.name=p2.name;
   p2.age=temp.age;
   p2.name=temp.name;
}

intmain(intargc,char**argv)
{
   Peoplep1("tom",18),p2("sam",19);
   swap(p1,p2);
   return0;
}


结果为:



这里分析下前面两个"h2"是通过调用构造函数People(std::stringn,inta)打印出来的,而"h3"就是通过调用复制构造函数People(constPeople&)而建立临时对象打印出来的,h1是调用默认构造函数People()打印出来的。那么怎么避免临时对象的建立呢?很简单,我们通过引用实参而达到目的

voidswap(People&p1,People&p2)
第三种情景就是函数返回对象时候。这里要注意临时对象的创建是通过复制构造函数构造出来的。

例如  constRationanloperator+(Rationanla,Rationanlb)该函数的返回值的临时的,因为它没有被命名,它只是函数的返回值。每回必须为调用add构造和释放这个对象而付出代价。

复制代码代码如下:
 #include<iostream>
 classRationanl
 {
    public:
        Rationanl(inte,intd)
        :_elemem(e),_denom(d)
        {
            std::cout<<"h2"<<std::endl;
        }
        voidshow()const;
        intelemem()const{return_elemem;}
        intdenom()const{return_denom;}
        void setElemon(inte){_elemon=e;}
        void setDenom(intd){_denom=d;}
        Rationanl(constRationanl&r);
        Rationanl&operator=(constRationanl&r);
    private:
        int_elemem;
        int_denom;
 };
 Rationanl::Rationanl(constRationanl&r)
 {
    setElemon(r.elemon());
    setDenom(r.denom());
    std::cout<<"h3"<<std::endl;
 }
 Rationanl&Rationanl::operator=(constRationanl&r)
 {
    setElemon(r.elemon());
    setDenom(r.denom());
    std::cout<<"h4"<<std::endl;
    return*this;
 }

 voidRationanl::show()
 {
        std::cout<<_elemen<<"/"<<_denom<<std::endl;
 }
 constRationanloperator*(constRationanllhs,constRationanlrhs)
 {
    returnRationalresult(lhs.elemen*rhs.elemen,rhs.denom*rhs.denom);
 }

 intmain(intargc,char**argv)
 {
    Rationanlr1(1,2),r2(1,3)
    Rationanlr3=r1*r2;   //GCC做了优化,没有看到临时变量。编译器直接跳过建立r3,使用赋值符号
    r3.show();
    //相当于 (r1*r2).show();
    return0;
 }

结果为:



这里很可惜没有看到我们想到看到的结果,结果应该为h2,h2,h2,h3,h4,应该是在返回值的时候有一个赋值构造函数,建立临时变量的,后来经笔者网上查找资料证实GCC做了优化。