构造函数以及拷贝构造函数区别以及explicit的对比

#include <iostream>

#include <string>

using namespace std;

 

class Str

{

    public:

    explicit Str(string cc = "josan")

    {

        cout << "f1 iscalled\n";

        str = cc;

    }

 

    explicit Str(Str& s)

    {

        cout << "f2 iscalled\n";

        str = s.str;

    }

 

    void print();

 

    ~Str()

    {

        cout << "Bye\n";

    }

 

    /*const Str& operator=(const Str& ss);*/

 

    private:

    string str;

 

};

 

void Str::print()

{

    cout << str << endl;

}

 

//const Str& Str::operator=(const Str& ss){

//  cout <<"f3 is called\n";

//  if (this ==&ss){

//      return*this;

//  }

//  this->str =ss.str;

//  return *this;

//}

 

int main()

{

    {

        //Str t;

        //Str t();

        //Str t{};

        //Str t{ "szs" };

        //Str t = { "szs" };

        //Str t = Str{ "szs" };

        //Str t("szs");

        //Str t = "szs";

        Str t = Str("szs");

       

 

 

        //t.print();

 

        //Str* st = new Str;

        //Str* st = new Str{};

        //Str* st = new Str();

        //Str* st = new Str{ "szs" };

        //Str* st = new Str("szs");

        //st->print();

    }

    cin.get();

    return 0;

 

}



以上是程序的测试代码主要考察构造函数以及复制构造函数的区别与联系。同时,加入explicit关键词的比较。通过测试代码,可以更好地理解上述概念。

测试环境为VS2013C++

下面的表格就是各个测试结果。

 


 


 

Str t;

Str t();

Str t{};

Str t{"szs"}

Str t={"szs"};

Str t=Str{"szs"}

Str t("szs")

Str t = "szs";

Str t = Str("szs");

Str(string cc = "josan")

√+Str(Str& s)

√+Str(Str& s)

explicit Str(string cc = "josan")

√+Str(Str& s)

√+Str(Str& s)

 

 

 

 

 

 

 

 

 

 

Str(Str& s)

 

 

 

 

 

 

explicit Str(Str& s)

 

 

 

 

 

 

备注:空格为无关 表示无法通过表示存在调用关系  √+Str(Str& s) 表示构造以及拷贝构造同时存在调用


 

 

Str* st = new Str;

Str* st = new Str{};

Str* st = new Str();

Str* st = new Str{ "szs" };

Str* st = new Str("szs");

Str(string cc = "josan")

explicit Str(string cc = "josan")

 

 

 

 

 

 

Str(Str& s)

 

 

 

 

 

explicit Str(Str& s)

 

 

 

 

 

备注:空格为无关 表示无法通过表示存在调用关系  √+Str(Str& s) 表示构造以及拷贝构造同时存在调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在引用的内容中,并没有提到explicit关键字可以直接修饰拷贝构造函数explicit关键字通常用于修饰类的构造函数,以防止隐式类型转换。在C++中,拷贝构造函数是一个特殊的构造函数,用于创建对象的副本。通常情况下,拷贝构造函数不会被explicit关键字修饰。所以,如果要强制禁止隐式调用拷贝构造函数,可以使用其他的方法,例如将拷贝构造函数声明为私有或删除该函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [explicit作用,拷贝构造函数,隐式类型](https://blog.csdn.net/NBE999/article/details/77881518)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【C++学习】explicit修饰构造函数](https://blog.csdn.net/TwT520Ly/article/details/80974757)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [c/c++拷贝构造函数和关键字explicit详解](https://download.csdn.net/download/weixin_38548589/13994418)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值