cpp operator = aka assignment operator overload

复制代码
//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.

 

posted @   FredGrit  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示