C++返回对象和引用的区别

30 31

C++返回对象和引用的区别

class A
{
punlic:
    A()
    {
        cout<<this<<" constructor"<<endl;
    }
    ~A()
    {
        cout<<this<<" destructor"<<endl;
    }
    A(const A & another)
    {
        cout<<this<<" cpy constructor from"<<&another<<endl;
    }   
    A& operator = (const A & another)
    {
        cout<<this<<" operator ="<<&another<<endl;
    }
};

//拷贝构造发生的时机
//1.构造新对象 A a ; A b = a;
//2.传参或返回对象

//对于普通变量来说,传引用效果不明显
//对于类对象而言,传对象效率很高
//传引用等价于 扩大了原对象的作用域

//栈上的对象是可以返回的,但不能返回栈上的引用(除非返回对象本身)
void func(A a)
{
    
}

int main()
{
    A x;
    A y = x;
    func(x);//发生一次拷贝构造  函数不是引用的话 引用的话  就没有拷贝构造了
    return 0;
}

A func(A &a)
{
    return a;
}

int main()//一次构造  一次拷贝构造 
{
    A x;
    A t = func(x);
    cout<<"&t "<<&t<<endl;
    return 0;
}

int main()//两次构造 一次拷贝构造 一次operator= 然后立马析构拷贝构造的临时变量
{
    A x;
    A t;
    t= func(x);
    cout<<"&t "<<&t<<endl;
    return 0;
}

A func()
{
    A b;
    return b;
}

int main()//两次拷贝 一次operator = 然后立马析构b的
{
    A t;
    t= func();
    cout<<"&t "<<&t<<endl;
    return 0;
}

加法 大于 小于 等于 [] at

class myString
{
public:
    myString(const char *p = NULL);//默认参数只能在声明
    myString(const myString & another);
    myString& operator=(const myString & another);
    ~myString();
    
    myString operator+(const myString &another );
    bool operator>(const myString &another );
    bool operator<(const myString &another );
    bool operator==(const myString &another );
    char& operator[](int idx);
    char at(int idx);
   
   char *c_str();
private:
    char *_str;
}

myString myString::operator+(const myString &another)
{
    myString tmp;//char *=""; *this char * = "abcd",another char*="efg"
    delete []tmp._str;
    int len = strlen(this->_str);
    len += strlen(another._str);
    
    tmp._str = new char[len+1];
    memset(tmp._str,0,len+1);
    strcat(tmp._str,this->_str);
    strcat(tmp._str,another._str);
    return tmp;
}
bool myString::operator>(const myString &another )
{
    if(strcmp(this->_str,another._str)>0)
        return true;
    else
        return false;
}
bool myString::operator<(const myString &another )
{
    if(strcmp(this->_str,another._str)<0)
        return true;
    else
        return false;
}
bool myString::operator==(const myString &another )
{
    if(strcmp(this->_str,another._str)==0)
        return true;
    else
        return false;
}
char& myString::operator[](int idx)//xx[2] xx.operator[](int idx)
{
    return this->_str[idx];
}
char myString::at(int idx)
{
    return this->_str[idx];
}
int main()
{
    string x= "abc",y="efg";
    string z= x+y;
    cout<<z.c_str()<<endl;

    myString xx="abcd",yy ="efg";
    myString zz= xx +yy;//本质 xx.operator+(yy);
   cout<<xx[2]<<endl;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、引用的基本作用是给变量起别名;2、引用必须初始化,引用在初始化后,不可以改变;3、函数传参时,(形参是对象时,...
    llllllllee阅读 1,824评论 0 0
  • 介绍 C语言是一门面向过程、抽象化的通用程序设计语言,广泛应用于底层开发。C语言能以简易的方式编译、处理低级存储器...
    Leon_520阅读 832评论 0 3
  • 前言: 详细介绍: List:元素有放入顺序,元素可重复Map:元素按键值对存储,无放入顺序Set:元素无放入顺序...
    YBshone阅读 8,717评论 0 17
  • C++类和对象 C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计。类是 C++ 的核心...
    863cda997e42阅读 691评论 0 4
  • C++基础入门 1 C++初识 1.1 第一个C++程序 编写一个C++程序总共分为4个步骤 创建项目 创建文件...
    雾中探雪阅读 107评论 0 0