//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