2

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

因为我是初学者,所以我写的几乎所有东西都会出错。编写移动构造函数的正确方法是什么?我的 2 个移动构造函数/移动赋值运算符应该是什么样子?

4

2 回答 2

3

您的构造函数不需要采取任何特殊操作,因此您可以使用构造函数合成版本:

CreditCard(const CreditCard& creditCard)=default;
CreditCard(CreditCard&& creditCard)=default;

等等。

如果你真的想实现它们,那么这是一个手工移动复制构造函数的例子。请注意,构造函数主体中没有任何事情发生。

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

关于复制分配操作符,如果您关心异常安全性,您可能需要查看复制和交换习语

于 2013-03-22T18:27:35.910 回答
0

如果你的班级成员是

std::string number;
int expMonth;
int expYear;
int pin;

并且您将复制构造函数编写为:

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

那么如果您自己没有指定编译器,这正是编译器将提供的

您只需要在某些特殊情况下编写复制构造函数,即当您必须动态分配指针并且您不希望指针仅以上述方式复制“值”时

注意:您没有提供初始化,std::string但我认为这不是故意的

于 2013-03-22T18:34:49.567 回答