C ++:隐式成员函数

高拉夫

考虑下面我编写的代码:

#include <iostream>

using namespace std;

class Sample{
    int a;
    public:
    Sample(){a=0;cout << "Sample Created (Default Constructor)" << endl;}
    Sample(int n):a(n){cout << "Sample Created" << endl;}
    ~Sample(){ cout << "Sample destroyed" << endl;}
    Sample(Sample& s){a=s.getA(); cout << "Sample Copy Constructor called" << endl;}
    Sample& operator= (Sample& s){this->a=s.getA(); cout << "Assignment Operator Called" << endl;return (*this);}
    void setA(int n){ a=n;}
    int getA(){return a;}
    };

class Test{
    Sample k;
    public:
    Test(){ cout << "Test Created(Default Constructor)" << endl;}
    Test(Sample& S):k(S){ cout << "Test Created" << endl;}
    ~Test(){cout << "Test Destroyed" << endl;}
    Test& operator= (Test& test){ k = test.getK(); cout << "Test Assignement Operator called" << endl; return (*this); } // Here 1
    Test(Test& test){k=test.getK();cout << "Test Copy Constructor called" << endl;}
    Sample getK(){return k;} // Here 2
    void setK(Sample& s){k=s;}
    };

int main()
{
    Sample a1(5);
    //Sample a2,a4;
    //a2=a1=a4;
    //Sample a3(a2);
    Test b1(a1);
    Test b2=b1;
    //b2=b1;

    return 0;
    }

编译时出现以下错误:

$ g++ -Wall Interview.cpp -o Interview
Interview.cpp: In member function `Test& Test::operator=(Test&)':
Interview.cpp:23: error: no match for 'operator=' in '((Test*)this)->Test::k = Test::getK()()'
Interview.cpp:12: note: candidates are: Sample& Sample::operator=(Sample&)
Interview.cpp: In copy constructor `Test::Test(Test&)':
Interview.cpp:24: error: no match for 'operator=' in '((Test*)this)->Test::k = Test::getK()()'
Interview.cpp:12: note: candidates are: Sample& Sample::operator=(Sample&)

当我对here 2as进行更改时Sample& getK(){return k;}它可以完美编译。

有人可以解释为什么吗?

此外,在here 1如果该函数被定义为Test& operator= (const Test& test){ k = test.getK(); cout << "Test Assignement Operator called" << endl; return (*this); }

我遇到错误-

$ g++ -Wall Interview.cpp -o Interview
Interview.cpp: In member function `Test& Test::operator=(const Test&)':
Interview.cpp:23: error: passing `const Test' as `this' argument of `Sample& Test::getK()' discards qualifiers

为什么这样?

juanchopanza

首先,您的副本构造函数和赋值运算符采用非const左值引用。例如,

Test& operator= (Test& test)

这意味着临时对象不能作为参数绑定到这些构造函数/运算符。const出于这个原因,规范签名使用引用,并且因为更改操作数没有意义:

Test& operator= (const Test& test)
                 ^^^^^

这将允许绑定到临时对象:

Test foo () { return Test(); }

Test t0;
t0 = foo();

其次,您的“获取器”必须为const,以便可以在const实例上或通过const指针或引用来调用它们

Sample getK() const {return k;}
              ^^^^^

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C++ 构造函数隐式成员

是否每个c ++成员函数都隐式地将“ this”作为输入?

C++在初始化类实例时隐式调用类成员的构造函数

带有成员参数列表的 C++ 隐式构造函数?

C 错误:函数和存储大小的隐式声明未知,尽管函数和结构是包含的头文件的成员

隐式定义的成员函数的生成是否为 C++ 中类层次结构中的每个类独立处理?

C ++构造函数隐式类型转换

-C中的隐式函数声明

C ++中的“隐式析构函数”

C ++ 11:默认构造函数:隐式还是显式?

C ++隐式和显式继承构造函数调用

C ++函数指针中的隐式类型转换?

函数“ DLog”的隐式声明在C99中无效

拦截C ++隐式副本构造函数,或调用其功能

为什么C ++隐式调用父默认构造函数?

free函数的隐式声明在c99中无效

构造函数中的C ++ 11隐式实例

C ++构造函数隐式转换未发生

警告:“函数'...'的隐式声明在C99中无效”

C ++构造函数中不需要的隐式转换

警告:函数的隐式声明在C99中无效?

杂项函数在C中进行隐式转换?

C ++隐式默认构造函数的意义是什么?

C ++将平凡可构造的结构隐式转换为成员

C隐式转换?

C ++类中的计算成员,由带有隐式转换超载的空结构成员组成

如何区分C ++中的隐式/显式构造函数调用?

C ++ 17:显式转换函数与显式构造函数+隐式转换-规则是否已更改?

C ++使用lambda进行隐式构造函数调用,期望使用函数指针