首页 > 其他分享 >cpp operator override = and ==

cpp operator override = and ==

时间:2023-01-22 19:11:59浏览次数:42  
标签:std set const string get operator book override cpp

//book.h
#pragma once
#ifndef __book_h__
#define __book_h__

#include <functional>
#include <iostream>

class book
{
public:
    book(const std::uint64_t& idx, const std::uint64_t& id, const std::string& abstract, const std::string& author,
     const std::string& comment,const std::string& content, const std::string& header, const std::string& isbn, 
     const std::string& summary, const std::string& title,const std::string& topic) : _idx(idx), _id(id), 
     _abstract(abstract), _author(author), _comment(comment),_content(content), _header(header), _isbn(isbn),
      _summary(summary), _title(title), _topic(topic) {}

    book() = default;
    ~book() = default;
    // copy constructor
    book(const book &bk);
    // move constructor
    book(book &&bk) = default;
    bool operator==(const book &another_book);
    void operator=(const book &bk);

    void set_idx(const std::uint64_t &idx);
    void set_id(const std::uint64_t &id);
    void set_abstract(const std::string &abstract);
    void set_author(const std::string &author);
    void set_comment(const std::string &comment);
    void set_content(const std::string &content);
    void set_header(const std::string &header);
    void set_isbn(const std::string &isbn);
    void set_summary(const std::string &summary);
    void set_title(const std::string &title);
    void set_topic(const std::string &topic);

    std::uint64_t get_idx() const;
    std::uint64_t get_id() const;
    std::string get_abstract() const;
    std::string get_author() const;
    std::string get_comment() const;
    std::string get_content() const;
    std::string get_header() const;
    std::string get_isbn() const;
    std::string get_summary() const;
    std::string get_title() const;
    std::string get_topic() const;

    void print_book(const book&bk);

private:
    std::uint64_t _idx;
    std::uint64_t _id;
    std::string _abstract;
    std::string _author;
    std::string _comment;
    std::string _content;
    std::string _header;
    std::string _isbn;
    std::string _summary;
    std::string _title;
    std::string _topic;
};
#endif

//book.cpp
#include "model/book.h"

void book::set_idx(const std::uint64_t& idx){
    _idx=idx;
}

void book::set_id(const std::uint64_t& id){
    _id=id;
}

void book::set_abstract(const std::string &abstract){
    _abstract=abstract;
}

void book::set_author(const std::string &author){
    _author=author;
}

void book::set_comment(const std::string& comment){
    _comment=comment;
}

void book::set_content(const std::string& content){
    _content=content;
}

void book::set_header(const std::string &header)
{
    _header=header;
}

void book::set_isbn(const std::string &isbn){
    _isbn=isbn;
}

void book::set_summary(const std::string &summary){
    _summary=summary;
}

void book::set_title(const std::string &title){
    _title=title;
}

void book::set_topic(const std::string &topic){
    _topic=topic;
}

std::uint64_t book::get_idx() const{
    return _idx;
}

std::uint64_t book::get_id() const{
    return _id;
}

std::string book::get_abstract() const{
    return _abstract;
}

std::string book::get_author() const{
    return _author;
}

std::string book::get_comment() const
{
    return _comment;
}

std::string book::get_content() const{
    return _content;
}

std::string book::get_header() const{
    return _header;
}

std::string book::get_isbn() const{
    return _isbn;
}

std::string book::get_summary() const{
    return _summary;
}

std::string book::get_title() const{
    return _title;
}

std::string book::get_topic() const{
    return _topic;
}

bool book::operator==(const book&bk){ 
    return this->_idx==bk.get_idx();
}

void book::operator=(const book &bk){
    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;
}


//model/util.h
#pragma once
#ifndef __util_h__
#define __util_h__

#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>
#include <threads.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <vector>
#include <jsoncpp/json/json.h>
#include "model/book.h"

class util
{
public:
    std::string get_time_now();
    std::string get_uuid();
    void print_log(std::string msg);
    void thread_le(int x, int y, int z, std::string str);
    void fill_book_vector(std::vector<book> &vec, const int& len);
    void print_book_vector(const std:: vector<book> &vec);
    void book_vector_fill_print(const int& len);
    void operator_override();
};

#endif

//model/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::thread_le(int x, int y, int z, std::string str)
{
    std::thread t1([](int xx, int yy, int zz, std::string sstr)
                   {
        util ul;
        for(int i=0;i<xx;i++){
            std::cout<<i+1<<","<<ul.get_uuid()<<","<<ul.get_time_now()<<std::endl;
            usleep(100000);
        }

        for(int i=0;i<yy;i++){
            std::cout<<ul.get_uuid()<<","<<ul.get_time_now()<<","<<i+1<<std::endl;
        }

        for(int i=0;i<zz;i++){
            std::cout<<ul.get_time_now()<<","<<ul.get_uuid()<<","<<i+1<<std::endl;
            usleep(100000);
        }

        std::cout<<sstr<<std::endl; },
                   x, y, z, str);
    t1.join();
    std::stringstream ss;
    ss << __FUNCTION__ << "," << __LINE__ << std::endl;
    print_log(ss.str());
}

void util::fill_book_vector(std::vector<book> &vec, const int& len){
    for(int i=0;i<len;i++)
    {
        vec.emplace_back(static_cast<std::uint64_t>(i),static_cast<std::uint64_t>(i*i*i*i*i),
        get_uuid(),get_uuid(),get_uuid(),get_uuid(),get_uuid(),get_uuid(),get_uuid(),
        get_uuid(),get_uuid());
    } 
}

void util::print_book_vector(const std::vector<book> &vec){
    std::vector<book>::const_iterator citr=vec.cbegin();
    while(citr!=vec.cend())
    {  
        std::cout<<std::fixed<<citr->get_idx()+1<<","<<citr->get_id()<<","<<citr->get_abstract()
        <<","<< citr->get_author()<<","<<citr->get_comment()<<","<<citr->get_content()<<","<<
        citr->get_header()<<","<<citr->get_isbn()<<","<<citr->get_summary()<<","<<
        citr->get_title()<<","<<citr->get_topic()<<std::endl<<std::endl;
        citr++;
    }
}

void util::book_vector_fill_print(const int& len){
    std::vector<book> vec;
    fill_book_vector(vec,len);
    print_book_vector(std::ref(vec));
    std::stringstream ss;
    ss << __FUNCTION__ << "," << __LINE__ << std::endl;
    print_log(ss.str());
}

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::stringstream ss;
    ss << __FUNCTION__ << "," << __LINE__ << std::endl;
    print_log(ss.str());
}

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

void print_uuid_time_number(int x, int y, int z, std::string str){
    util ul;
    ul.thread_le(x, y, z, str);
}

void fill_print_book_vector(int len){
    util ul;
    ul.book_vector_fill_print(len);
}

void operator_override_demo()
{
    util ul;
    ul.operator_override();
}

int main(int args, char **argv){
    try
    {
        /* code */
        operator_override_demo();
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }
}
//compile via g++
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread

Run

./h

 

 When we scrutinize the above snapshot,when used overrided  assignment operator,it print the identical content such as the shallow copy.

The hotspot as below.

void book::operator=(const book &bk){
    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;
}

 

I dug into deeper as  below.

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<<"The original address "<<&bk<<std::endl;
    std::cout<<"The assigned address "<<&another_book<<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
./h1

 

 

Scrutinize the above screenshot,the magic is the different address,which looks like deep copy.

 

标签:std,set,const,string,get,operator,book,override,cpp
From: https://www.cnblogs.com/Fred1987/p/17064579.html

相关文章

  • [Typescript 4.9] TypeScript 4.9: satisfies operator
    Previously,wehaveproblemforsuchcode:typeRGB=readonly[red:number,green:number,blue:number];typeColor={value:RGB|string};constmyColor......
  • 类内函数的override问题-方法
    Question:haveabaseclasswithavirtualfunction:classBase{public:virtualvoidFunction();};voidBase::Function(){cout<<"defaultversion"<<......
  • 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语言来编......
  • Windbg 打开CPP文件 中文乱码
    Windbg打开CPP文件中文乱码Windbg在调试的时候如果源代码里面有中文会乱码本质上不是Windbg的问题而是代码源文件的编码问题,默认情况下VS生成的代码文件的编码都是ANSI......
  • java 重写equals方法 最佳实践 Java@Override equals():当this.getClass()!=o.getClass()
    java重写equals方法最佳实践Java@Overrideequals():当this.getClass()!=o.getClass()失败https://www.5axxw.com/questions/content/dn5jai我在MyClass中有一个@Overrid......
  • cpp之智能指针
    1.介绍本文介绍智能指针的使用。智能指针是c++中管理资源的一种方式,用智能指针管理资源,不必担心资源泄露,将c++程序员从指针和内存管理中解脱出来,再者,这也是c++发展的趋......
  • Dev-Cpp 下载与安装 [ 图文教程 ]
    版权声明©**本文作者:main工作室本文链接:https://www.cnblogs.com/main-studio/p/16937530.html版权声明:本文为「main工作室」的原创文章,遵循CCBY-NC-SA4.0版权协......