复制构造函数不工作?

时间:2013-04-11 06:46:21

标签: 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;
}

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;
}

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;
}

3 个答案:

答案 0 :(得分:7)

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

//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;
}

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

(当然,这适用于所有你的课程。)

答案 1 :(得分:4)

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

答案 2 :(得分:2)

在调用复制构造函数时,您当然必须将值分配给要创建的对象...

Potion::Potion(const Potion & copy)
{
   //assign the elements from copy to your object here
   std::cout << "Potion's copy constructor " << std::endl;
}

复制构造函数不会为您执行此操作。