首页 > 其他分享 >类内函数

类内函数

时间:2023-07-19 12:44:33浏览次数:41  
标签:调用 函数 运算符 类内 MyClass 构造函数

   创建了一个名为MyClass的类,并在其中实现了默认构造函数参数化构造函数拷贝构造函数、移动构造函数、析构函数、拷贝赋值运算符、移动赋值运算符、成员函数、静态成员函数和友元函数。在主函数中,我们创建了几个类对象,并演示了这些函数的调用和使用。请注意,输出语句被添加到每个函数的实现中,以便在调用时打印出相应的消息,以便我们可以清楚地看到每个函数的调用顺序

 

class MyClass {
public:
    int value;

    // Default constructor
    MyClass() {
        value = 0;
        cout << "Default constructor called" << endl;
    }

    // Parameterized constructor
    MyClass(int val) {
        value = val;
        cout << "Parameterized constructor called" << endl;
    }

    // Copy constructor
    MyClass(const MyClass& other) {
        value = other.value;
        cout << "Copy constructor called" << endl;
    }

    // Move constructor
    MyClass(MyClass&& other) noexcept {
        value = other.value;
        cout << "Move constructor called" << endl;
    }

    // Destructor
    ~MyClass() {
        cout << "Destructor called" << endl;
    }

    // Copy assignment operator
    MyClass& operator=(const MyClass& other) {
        if (this != &other) {
            value = other.value;
        }
        cout << "Copy assignment operator called" << endl;
        return *this;
    }

    // Move assignment operator
    MyClass& operator=(MyClass&& other) noexcept {
        value = other.value;
        cout << "Move assignment operator called" << endl;
        return *this;
    }

    // Member function
    void printValue() {
        cout << "Value: " << value << endl;
    }

    // Static member function
    static void staticFunction() {
        cout << "Static function called" << endl;
    }

    // Friend function
    friend void friendFunction(const MyClass& obj);
};

void friendFunction(const MyClass& obj) {
    cout << "Friend function called. Value: " << obj.value << endl;
}

int main()
{
    MyClass obj1;  // Default constructor called
    obj1.value = 10;

    MyClass obj4(20);

    MyClass obj2(obj1);  // Copy constructor called

    MyClass obj3 = std::move(obj4);  // Move constructor called

    obj2 = obj3;  // Copy assignment operator called

    obj3 = std::move(obj2);  // Move assignment operator called

    obj3.printValue();  // Value: 10

    MyClass::staticFunction();  // Static function called

    friendFunction(obj3);  // Friend function called. Value: 10

    return 0;
}

  

标签:调用,函数,运算符,类内,MyClass,构造函数
From: https://www.cnblogs.com/susiesnai-sun/p/17565271.html

相关文章

  • 大语言模型的预训练[5]:语境学习、上下文学习In-Context Learning:精调LLM、Prompt设计
    大语言模型的预训练[5]:语境学习、上下文学习In-ContextLearning:精调LLM、Prompt设计和打分函数(ScoringFunction)设计以及ICL底层机制等原理详解1.In-ContextLearning背景与定义背景大规模预训练语言模型(LLM)如GPT-3是在大规模的互联网文本数据上训练,以给定的前缀来预测生......
  • coc仓库--minitouch控制函数封装
    minitouch控制函数封装minitouch的github地址:1.原函数voidclick(FILE*wirteFile,conststd::string*ADB_IP,intx,inty){std::strings="d0"+std::to_string(x)+""+std::to_string(y)+""+"50\n";fwrite(s......
  • Mysql基础6-常用数据库函数
    一、字符串函数1、常见Mysql内置字符串函数concat(s1,s2,s3,...):字符串拼接,将s1,s2,s3...等拼接成一个字符串lower(str):将字符串str全部转为小写upper(str):将字符串str全部转为大写lpad(str,n,pad):左填充,将字符串pad对str的左边进行填充,达到n个字符串长度rpad(str,n,......
  • 色彩解锁:探索革命性的CSS color()函数和新的色彩空间
    Google在6月份发布了一篇新博客,介绍了CSS中的新颜色空间和函数,支持所有主流引擎。下面是文章的链接:NewCSScolorspacesandfunctionsinallmajorengineshttps://web.dev/color-spaces-and-functions/?ref=sidebar该文章展示了一些支持的色彩空间的例子。color()函数介绍:color......
  • coc仓库--minicap截图函数
    minicap截图1.原函数voidscreenShot(conststd::string*ADB_IP,cv::Mat*mat){//首先,运行runShellAndReturn获取file指针std::stringcmd="adb-s"+*ADB_IP+"shellLD_LIBRARY_PATH=/data/local/tmp/data/local/tmp/minicap-P1920x1080@1920x1......
  • Java中的split( )函数
      Java中的split()函数Leaf_Ysm关注IP属地:浙江0.1512018.10.2410:44:46字数252阅读46,821首先,我们来了解一下split()函数各个参数的意义publicString[]split(Stringregex,intlimit)regex--正则表达式分隔符。limit--分割的份数。下面就让我......
  • CSS中关于Calc 函数的使用规则
    calc()函数用于动态计算长度值。需要注意的是,运算符前后都需要保留一个空格,例如:width:calc(100%-10px);任何长度值都可以使用calc()函数进行计算;calc()函数支持"+","-","*","/"运算;calc()函数使用标准的数学运算优先级规则;......
  • poj 2311 Cutting Game (sg函数)
    小记:这题是对sg函数的初步理解。对于sg函数只要g[x]==0,则此时为必败态。x作为后继,我们就要对所有的后继进行标记,然后mex之。因为每次只能切一刀,所以切完之后,会有两块方格,而对每一块方格进行游戏又会有一个sg函数值,所以根据sg函数的性质,它这一刀所代表的后继,即为这两块方格的sg函......
  • Java使用Stream函数对集合进行分组
    1List<Map<String,String>>list=newArrayList<>();2Map<String,String>map1=newHashMap<>();3map1.put("name","卢俊义");4map1.put("book","水浒传"......
  • 解决调用Lib时无法找到其导出函数的问题
    问题:一个工程导出静态lib,另一个工程使用Lib中的函数,但是在编译时无法找到Lib中函数,使用dumpbin查看Lib确实导出函数了。解决:确保导出工程和使用工程的调用约定要一致,即C/C++->Advanced->CallingConvention的选项要相同,如都是__stdcall,这样调用时就不会有问题了。......