复制构造函数不工作?

MrP*_*le5 0 c++ oop copy-constructor

OOP有些新东西(即C程序员转换为C++)并且无法弄清楚为什么我的背包类中的数据成员是空的.我向我的背包传递了一系列药水,但数据成员说mType =""(即什么都没有).

我以前从来没有觉得这在节目中输了.开始讨厌OOP(即开玩笑......但这非常令人沮丧).

main.cpp中

#include <iostream>
#include "rogue.h"
#include "weapon.h"
#include "backpack.h"
#include "potion.h"
#include "coinpouch.h"

int main()
{
Potion myFavoritePotions[5];
myFavoritePotions[0].setName("love");
myFavoritePotions[1].setName("hate");
myFavoritePotions[2].setName("shrink");
myFavoritePotions[3].setName("grow");
myFavoritePotions[4].setName("disappear");
BackPack myFavoriteBackPack(myFavoritePotions);
Weapon myFavoriteWeapon("AK-47");
Weapon mySecretWeapon("Me-262");
Weapon myLeastFavoriteWeapon("Luger");
CoinPouch myFavoritePurse(6,5,4,3);
Rogue myFavoriteRogue("Cynic", myFavoriteWeapon, mySecretWeapon, myFavoriteBackPack, myFavoritePurse);

mySecretWeapon = myFavoriteWeapon;  

myFavoriteRogue.setOffHand(myLeastFavoriteWeapon);
//std::cout << myFavoriteRogue.getOffHand();

return 0;
}
Run Code Online (Sandbox Code Playgroud)

potion.cpp

#include <iostream>
#include "potion.h"

//Manager function definitions

//Default constructor
Potion::Potion()
{}

//Constructor
Potion::Potion(std::string name)
:mName(name)
{
std::cout << "Potion's constructor " << std::endl;
}

//Destructor
Potion::~Potion()
{
std::cout << "Potion's destructor " << std::endl;
}

//Copy constructor
Potion::Potion(const Potion & copy)
{
std::cout << "Potion's copy constructor " << std::endl;
}

//Overloaded assignment operator
Potion &Potion::operator= (const Potion & rhs)
{
std::cout << "Potion's overloaded assignment operator. " << std::endl;

return *this;
}

//Setters
void Potion::setName(std::string name)
{
mName = name;
}

//Getters
std::string Potion::getName()
{
return mName;
}
Run Code Online (Sandbox Code Playgroud)

backpack.cpp

#include <iostream>
#include "backpack.h"

//Manager function definitions

//Default constructor
BackPack::BackPack()
{}

//Constructor
BackPack::BackPack(Potion Potions[])
{
for(int i = 0; i < 5; i++)
{
    mPotions[i] = Potions[i];
}
std::cout << "Backpack's constructor. " << std::endl;
}

//Destructor
BackPack::~BackPack()
{
std::cout << "Backpack's destructor. " << std::endl;
}

//Copy constructor
BackPack::BackPack(const BackPack & copy)
{
std::cout << "Backpack's copy constructor. " << std::endl;
}

//Overloaded assignment operator
BackPack &BackPack::operator=(const BackPack & rhs)
{
std::cout << "Backpack's assignment operator. " << std::endl;

return *this;
}

//Setters
void BackPack::setPotion(Potion Potions[])
{
for(int i = 0; i < 5; i++)
{
    mPotions[i] = Potions[i];
}
}

//Getters
Potion * BackPack::getPotion()
{
Potion * potionPointer = mPotions;
return potionPointer;
}
Run Code Online (Sandbox Code Playgroud)

NPE*_*NPE 7

您的复制构造函数不进行任何复制:

//Copy constructor
BackPack::BackPack(const BackPack & copy)
{
std::cout << "Backpack's copy constructor. " << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

您的副本分配运算符也不是:

//Overloaded assignment operator
BackPack &BackPack::operator=(const BackPack & rhs)
{
std::cout << "Backpack's assignment operator. " << std::endl;

return *this;
}
Run Code Online (Sandbox Code Playgroud)

一旦提供了自己的复制构造函数和复制赋值运算符,就会抑制编译器生成的运算符.如果您需要进行一些复制,则必须实现它.

(当然,这适用于所有课程.)


Som*_*ude 5

如果您实现了复制构造函数和复制赋值运算符,那么您实际上必须实现复制。它不会为你完成。