c++17(16)-vector 元素为类对象,复制构造函数,const对象

#include <iostream>
#include <cstring> 
#include <vector>
using namespace std;

class Book
{
private:
    int _id;
    char* _name;
    float _price;
    static inline int nowid{0};
    int _bookCount{1};
public : 
    Book(int id, char* name, float price)
        : _id(id)
        , _price(price)
    {
        int nameLen = strlen(name);
        _name = new char[nameLen + 1];
        strcpy(_name, name);
    }
    Book(char* name, float price):Book(nowid,name,price){
        nowid++;
    }
    Book(const Book &otherBook):Book(otherBook.nowid,otherBook._name,otherBook._price){
        nowid++;
    }    
    ~Book()
    {
        delete[] _name;
    }
    friend const Book& operator +(const Book& leftBook,const Book& rightBook){
        if (leftBook==rightBook) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount+rightBook._bookCount;
            return *book;
        }
        else{
            return leftBook;
        }
    }
    friend const Book& operator -(const Book& leftBook,const Book& rightBook){
        if (leftBook==rightBook) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount-rightBook._bookCount;
            if ( book->_bookCount<0) book->_bookCount=0;
            return *book;
        }
        else{
            return leftBook;
        }        
    }    
    Book& operator -=(const Book& otherBook){
        if (*this==otherBook) {
            _bookCount-=otherBook._bookCount;
            if ( _bookCount<0) _bookCount=0;
            return *this;
        }
        else{
            return *this;
        }        
    }      
    Book& operator +=(const Book& otherBook){
        if (*this==otherBook) {
            _bookCount+=otherBook._bookCount;
            return *this;
        }
        else{
            return *this;
        }        
    }    
    friend const bool operator ==(const Book& leftBook,const Book& rightBook){
        if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {
            return true;
        }
        else{
            return false;
        }        
    } 
    friend  const bool operator !=(const Book& leftBook,const Book& rightBook){
        if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {
            return false;
        }
        else{
            return true;
        }        
    }    
    void print(){
        cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;
    }
    void printCount(){
         cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl;       
    }
};
class BookS{
private:
    vector<Book> _myBooks;
public:
    void addBook(const Book &book){
        _myBooks.push_back(book);   
    }
    void print(){
        for( int i = 0; i < _myBooks.size(); i++ ){
            _myBooks[i].print();
        }
    }
    
};

int main(int argc, char **argv)
{
   Book *book1=new Book("精通Python",62.37);  
   Book *book2=new Book("精通C++",49.21);     
   BookS myBks;
   myBks.addBook(*book2); 
   myBks.addBook(*book1); 
   myBks.print();
 
    
}
4,精通C++,49.21==>1
3,精通Python,62.37==>1

Hit any key to continue...

如果不定义复制构造函数,程序会出现异常

Book(const Book &otherBook):Book(otherBook.nowid,otherBook._name,otherBook._price){
    nowid++;
}    

因为下面语句将生成新的Book对象,默认复制构造函数会导致浅复制,从而导致浅复制,字符指针
char* _name为指针类型,必须使用深复制

void addBook(const Book &book){
    _myBooks.push_back(book);   
}

C++中const 引用的是对象时只能访问该对象的const 函数,使用for遍历时,如果指定为const Book &类型,则需要将print定义为const

#include <iostream>
#include <cstring> 
#include <vector>
using namespace std;

class Book
{
private:
    int _id;
    char* _name;
    float _price;
    static inline int nowid{0};
    int _bookCount{1};
public : 
    Book(int id, char* name, float price)
        : _id(id)
        , _price(price)
    {
        int nameLen = strlen(name);
        _name = new char[nameLen + 1];
        strcpy(_name, name);
    }
    Book(char* name, float price):Book(nowid,name,price){
        nowid++;
    }
    Book(const Book &otherBook):Book(otherBook.nowid,otherBook._name,otherBook._price){
        nowid++;
    }    
    ~Book()
    {
        delete[] _name;
    }
    friend const Book& operator +(const Book& leftBook,const Book& rightBook){
        if (leftBook==rightBook) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount+rightBook._bookCount;
            return *book;
        }
        else{
            return leftBook;
        }
    }
    friend const Book& operator -(const Book& leftBook,const Book& rightBook){
        if (leftBook==rightBook) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount-rightBook._bookCount;
            if ( book->_bookCount<0) book->_bookCount=0;
            return *book;
        }
        else{
            return leftBook;
        }        
    }    
    Book& operator -=(const Book& otherBook){
        if (*this==otherBook) {
            _bookCount-=otherBook._bookCount;
            if ( _bookCount<0) _bookCount=0;
            return *this;
        }
        else{
            return *this;
        }        
    }      
    Book& operator +=(const Book& otherBook){
        if (*this==otherBook) {
            _bookCount+=otherBook._bookCount;
            return *this;
        }
        else{
            return *this;
        }        
    }    
    friend const bool operator ==(const Book& leftBook,const Book& rightBook){
        if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {
            return true;
        }
        else{
            return false;
        }        
    } 
    friend  const bool operator !=(const Book& leftBook,const Book& rightBook){
        if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {
            return false;
        }
        else{
            return true;
        }        
    }    
    const void print() const{
        cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;
    }
    void printCount(){
         cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl;       
    }
};
class BookS{
private:
    vector<Book> _myBooks;
public:
    void addBook(const Book &book){
        _myBooks.push_back(book);   
    }
    void print() {
        for(const Book &bk:_myBooks){
            bk.print();
        }
    }
    
};

int main(int argc, char **argv)
{
   Book *book1=new Book("精通Python",62.37);  
   Book *book2=new Book("精通C++",49.21);     
   BookS myBks;
   myBks.addBook(*book2); 
   myBks.addBook(*book1); 
   myBks.print();
 
    
}

如果定义vecotr时,使用指针做为其元素,则一切正常,无需定义复制构造函数,指针元素使用浅复制即可。

#include <iostream>
#include <cstring> 
#include <vector>
using namespace std;

class Book
{
private:
    int _id;
    char* _name;
    float _price;
    static inline int nowid{0};
    int _bookCount{1};
public : 
    Book(int id, char* name, float price)
        : _id(id)
        , _price(price)
    {
        int nameLen = strlen(name);
        _name = new char[nameLen + 1];
        strcpy(_name, name);
    }
    Book(char* name, float price):Book(nowid,name,price){
        nowid++;
    }

    ~Book()
    {
        delete[] _name;
    }
    friend const Book& operator +(const Book& leftBook,const Book& rightBook){
        if (leftBook==rightBook) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount+rightBook._bookCount;
            return *book;
        }
        else{
            return leftBook;
        }
    }
    friend const Book& operator -(const Book& leftBook,const Book& rightBook){
        if (leftBook==rightBook) {
            Book *book=new Book(rightBook._name, rightBook._price); 
            book->_bookCount=leftBook._bookCount-rightBook._bookCount;
            if ( book->_bookCount<0) book->_bookCount=0;
            return *book;
        }
        else{
            return leftBook;
        }        
    }    
    Book& operator -=(const Book& otherBook){
        if (*this==otherBook) {
            _bookCount-=otherBook._bookCount;
            if ( _bookCount<0) _bookCount=0;
            return *this;
        }
        else{
            return *this;
        }        
    }      
    Book& operator +=(const Book& otherBook){
        if (*this==otherBook) {
            _bookCount+=otherBook._bookCount;
            return *this;
        }
        else{
            return *this;
        }        
    }    
    friend const bool operator ==(const Book& leftBook,const Book& rightBook){
        if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {
            return true;
        }
        else{
            return false;
        }        
    } 
    friend  const bool operator !=(const Book& leftBook,const Book& rightBook){
        if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {
            return false;
        }
        else{
            return true;
        }        
    }    
    const void print() const{
        cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;
    }
    void printCount(){
         cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl;       
    }
};
class BookS{
private:
    vector<Book*> _myBooks;
public:
    void addBook(Book* book){
        _myBooks.push_back(book);   
    }
    void print() {
        for(const Book *bk:_myBooks){
            bk->print();
        }
    }
    
};

int main(int argc, char **argv)
{
   Book *book1=new Book("精通Python",62.37);  
   Book *book2=new Book("精通C++",49.21);     
   BookS myBks;
   myBks.addBook(book2); 
   myBks.addBook(book1); 
   myBks.print();
 
    
}
发布了385 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/AI_LX/article/details/104473783