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

cpp operator = aka assignment operator overload

 

 

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.

cpp operator = aka assignment operator overload

 

原文链接: https://www.cnblogs.com/Fred1987/p/17064591.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    cpp operator = aka assignment operator overload

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/313585

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午12:57
下一篇 2023年2月16日 下午12:58

相关推荐