1

我有这两个简单的类:CreditCard 和 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;
    }
};

主要是我有这条线:

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

怎么了:

  1. CreditCard 构造函数被调用
  2. CreditCard 移动构造函数被调用
  3. 帐户构造函数被调用
  4. CreditCard 复制构造函数被调用

我不明白为什么会发生4。为什么调用复制构造函数?

PS:对这些课程的任何更正表示赞赏。

4

2 回答 2

7

我不明白为什么会发生4。为什么调用复制构造函数?

那是因为这条线:

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

在 的构造函数中Account,您将左值复制creditCard到成员变量creditCard中。这会导致调用CreditCard.

实际上,既然你是按价值取值,那么移动creditCard它是安全的:

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

这将导致调用第 4 步中的移动构造函数CreditCard而不是复制构造函数。

于 2013-03-22T18:04:41.023 回答
2
  1. CreditCard("12345", 1, 2014, 1122)被构造。让我们称之为c1
  2. c1被移到构造函数的creditCard参数中Account
  3. 开工建设account1
  4. account1.creditCardcreditCard根据成员初始化程序列表,从构造函数参数复制构造。
于 2013-03-22T18:05:22.813 回答