首页 > 其他分享 >cpp operator = aka assignment operator overload

cpp operator = aka assignment operator overload

时间:2023-01-22 19:33:19浏览次数:34  
标签:std set uuid get bk cpp book operator aka

//model/book.cpp
void book::operator=(const book &bk){
    std::cout<<std::endl<<std::endl<<std::endl<<"Called operator assignment overloaded!"
    <<std::endl<<std::endl<<std::endl;
    this->set_idx(bk.get_idx());
    this->set_id(bk.get_id());
    this->set_abstract(bk.get_abstract());
    this->set_author(bk.get_author());
    this->set_comment(bk.get_comment());
    this->set_content(bk.get_content());
    this->set_header(bk.get_header());
    this->set_isbn(bk.get_isbn());
    this->set_summary(bk.get_summary());
    this->set_title(bk.get_title());
    this->set_topic(bk.get_topic());
}

void book::print_book(const book&bk){
    std::cout<<std::fixed<<bk.get_idx()<<","<<bk.get_id()<<","<<bk.get_abstract()<<","<<bk.get_author()
    <<","<<bk.get_comment()<<","<<bk.get_content()<<","<<bk.get_header()<<","<<bk.get_isbn()<<","
    <<bk.get_summary()<<","<<bk.get_title()<<","<<bk.get_topic()<<std::endl;
}

//util.cpp
#include "util.h"

std::string util::get_time_now()
{
    std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *std::localtime(&raw_time);
    std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
    uint64_t mills_count = mills.count() - seconds.count() * 1000;
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S") << std::setfill('0') << std::setw(3) << mills_count;
    return ss.str();
}

std::string util::get_uuid()
{
    uuid_t new_uuid;
    char *uuid_value = (char *)malloc(40);
    uuid_generate(new_uuid);
    uuid_unparse(new_uuid, uuid_value);
    std::string dt_now(uuid_value);
    free(uuid_value);
    uuid_value = nullptr;
    return dt_now;
}

void util::print_log(std::string msg)
{
    std::cout << get_time_now() << ",finished in " << msg << std::endl;
}

void util::operator_override(){
    book bk(static_cast<uint64_t>(10*10),static_cast<uint64_t>(10*10),get_uuid(),get_uuid(),get_uuid(),
    get_uuid(),get_uuid(),get_uuid(),get_uuid(),get_uuid(),get_uuid());
    bk.print_book(bk);
    book another_book;
    another_book=bk;
    another_book.print_book(another_book);
    std::cout<<std::endl<<std::endl;
    std::cout<<"The original address "<<&bk<<std::endl;
    std::cout<<"The assigned address "<<&another_book<<std::endl;
    std::cout<<std::endl<<std::endl;
    std::stringstream ss;
    ss << __FUNCTION__ << "," << __LINE__ << std::endl;
    print_log(ss.str());
}

 

 

Compile

g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread

 

Run

 

 

When look into the above snapshot,it had invoked the overloaded assignment method and print the identical information.

But to my surprise,their addresses are different as below,think further,because they are different objects and called constructor respectively.

 

标签:std,set,uuid,get,bk,cpp,book,operator,aka
From: https://www.cnblogs.com/Fred1987/p/17064591.html

相关文章

  • cpp operator override = and ==
    //book.h#pragmaonce#ifndef__book_h__#define__book_h__#include<functional>#include<iostream>classbook{public:book(conststd::uint64_t&idx......
  • [Typescript 4.9] TypeScript 4.9: satisfies operator
    Previously,wehaveproblemforsuchcode:typeRGB=readonly[red:number,green:number,blue:number];typeColor={value:RGB|string};constmyColor......
  • Win 11系统安装提示:若要获取疑难解答提示,请使用其他设备并访问aka.ms/networksetup
      001、win11安装过程中遇到如下问题:    002、先把网线先拔了,按下键盘上的Shift+F10(笔记本可能是Shift+FN+F10)调出系统的cmd功能,然后输入oobe\bypa......
  • K8S Operator的开发与使用
    从应用角度考虑,为什么会出现如此多的Operator场景,为什么很多中间件和厂商都会提供基于Operator的部署方案,他的价值是什么?随着时代的发展,企业应用部署环境从传统的物理机->......
  • promise-cpp应用--01简单应用
    promise-cpp是一种C++promise/A+库#include<iostream>#include<future>#include<string>#include<sstream>#include<stdexcept>#include<functional>#inclu......
  • Prometheus Operator配置Alertmanager告警
    1、管理Alertmanagerconfiguration1.1方式一,使用存储在Kubernetessecret中的本地Alertmanager配置文件1、编写alertmanager配置alertmanager.yamlroute:group_by......
  • C 语言初学者必备开发工具——Dev-Cpp [ 图文安装教程 ]
    前言C语言是一门功能强大的专业化编程语言,深受专业程序员和业余编程爱好者的喜爱,同时C语言也是当今最流行的嵌入式开发语言。大多数嵌入式项目的开发都是用C语言来编......
  • free5gc+UERANSIM的5GAKA源码和抓包分析
    5G_AKA的整体流程 由上图可以看到5G_AKA的验证流程就是首先UE验证核心网,通过后核心网在对UE进行验证的一个认证注册流程。核心网UDM产生鉴权向量AV并传输(Authenticat......
  • Windbg 打开CPP文件 中文乱码
    Windbg打开CPP文件中文乱码Windbg在调试的时候如果源代码里面有中文会乱码本质上不是Windbg的问题而是代码源文件的编码问题,默认情况下VS生成的代码文件的编码都是ANSI......
  • cpp之智能指针
    1.介绍本文介绍智能指针的使用。智能指针是c++中管理资源的一种方式,用智能指针管理资源,不必担心资源泄露,将c++程序员从指针和内存管理中解脱出来,再者,这也是c++发展的趋......