简体   繁体   中英

Why is copy constructor called in this case?

I have these two simple classes: CreditCard and Account

    #pragma once

    #include<string>

    class CreditCard
    {
    private:
        std::string number;
        int expMonth;
        int expYear;
        int pin;
    public:

        CreditCard():number(""), expMonth(0), expYear(0), pin(0)
        {

        }

        CreditCard(std::string number, int expMonth, int expYear,int pin):number(number), expMonth(expMonth), expYear(expYear), pin(pin)
        {
            if(number.length()!=5)
            {
                throw std::invalid_argument("Card number must be 5 characters");
            }
            if(expMonth<0 || expMonth>12)
            {
                throw std::invalid_argument("Invalid expiration month");
            }
            if(expYear<2014 || expYear>2020)
            {
                throw std::invalid_argument("Invalid expiration year");
            }
            if(pin<1000 || pin>9999)
            {
                throw std::invalid_argument("PIN must be between 1000 and 9999");
            }
        }

        CreditCard(const CreditCard &creditCard):number(creditCard.number), expMonth(creditCard.expMonth), expYear(creditCard.expYear), pin(creditCard.pin)
        {

        }   

        CreditCard(CreditCard &&creditCard):number(std::move(creditCard.number)), expMonth(std::move(creditCard.expMonth)), expYear(std::move(creditCard.expYear)), pin(std::move(creditCard.pin))
        {
            creditCard.number="";
            creditCard.expMonth=0;
            creditCard.expYear=0;
            creditCard.pin=0;
        }

        CreditCard& operator = (const CreditCard &creditCard)
        {
            number=creditCard.number;
            expMonth=creditCard.expMonth;
            expYear=creditCard.expYear;
            pin=creditCard.pin;
            return *this;
        }

        CreditCard& operator = (CreditCard &&creditCard)
        {
            if(this!=&creditCard)
            {
                number=std::move(creditCard.number);
                expMonth=std::move(creditCard.expMonth);
                expYear=std::move(creditCard.expYear);
                pin=std::move(creditCard.pin);
                number=creditCard.number;
                expMonth=creditCard.expMonth;
                expYear=creditCard.expYear;
                pin=creditCard.pin;
            }
            return *this;
        }

        std::string getNumber() const
        {
            return number;
        }

        int getExpMonth() const
        {
            return expMonth;
        }

        int getExpYear() const
        {
            return expYear;
        }

        int getPin() const
        {
            return pin;
        }
    };


#pragma once

#include<string>
#include"CreditCard.h"

class Account
{
private:
    std::string number;
    float amount;
    CreditCard creditCard;
public:
    Account():number(""), amount(0), creditCard()
    {

    }

    Account(std::string number, float amount, CreditCard creditCard) : number(number), amount(amount), creditCard(creditCard)
    {

    }

    Account(const Account &account) : number(account.number), amount(account.amount), creditCard(account.creditCard)
    {

    }

    Account(Account &&account) : number(std::move(account.number)), amount(std::move(account.amount)), creditCard(std::move(creditCard))
    {

    }

    Account& operator = (const Account &account)
    {
        number=account.number;
        amount=account.amount;
        creditCard=account.creditCard;      
        return *this;
    }

    Account& operator = (Account &&account)
    {
        if(this!=&account)
        {
            number=account.number;
            amount=account.amount;
            creditCard=account.creditCard;

            account.number="";
            account.amount=0;
            account.creditCard=CreditCard("",0,0,0);
        }
        return *this;
    }

    std::string getNumber() const
    {
        return number;
    }

    float getAmount() const
    {
        return amount;
    }

    void deposit(const float &depositAmount)
    {
        amount+=depositAmount;
    }

    void withdraw(const float &withdrawAmount)
    {
        amount-=withdrawAmount;
    }
};

And in main i have this line:

Account account1("x12345x",1235.2, CreditCard("12345",1,2014,1122));

What is happening:

  1. CreditCard constructor gets called
  2. CreditCard move constructor gets called
  3. Account constructor gets called
  4. CreditCard copy constructor gets called

I don't understand why 4 is happening. Why that copy constructor call?

PS:Any correction regarding those classes is appreciated.

I don't understand why 4 is happening. Why that copy constructor call?

That is because of this line:

Account(std::string number, float amount, CreditCard creditCard) 
: 
number(number), amount(amount), creditCard(creditCard)
//                              ^^^^^^^^^^^^^^^^^^^^^^
{ ... }

In the constructor of Account , you are copying the lvalue creditCard into the member variable creditCard . This causes a call to the copy constructor of CreditCard .

Actually, since you are taking creditCard by value, it is safe to move it:

Account(std::string number, float amount, CreditCard creditCard) 
: 
number(number), amount(amount), creditCard(std::move(creditCard))
//                                         ^^^^^^^^^^^^^^^^^^^^^
{ ... }

This will cause a call to the move constructor of CreditCard rather than to the copy constructor in step 4.

  1. CreditCard("12345", 1, 2014, 1122) is constructed. Let's call it c1 .
  2. c1 gets moved into the creditCard parameter of Account constructor.
  3. Construction of account1 starts.
  4. account1.creditCard is copy-constructed from the constructor parameter creditCard , as per the member initialiser list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM