指针问题?这个问题?类问题

Pointer issue? This issue? class issue?

本文关键字:问题 指针      更新时间:2023-10-16
#include <iostream>
using namespace std;
class Item
{
private:
   string name;
  int power;
  int durability;
public:
  Item(string n, int p, int d);
  void Describe() const;
  ~Item();
};
Item::Item(string n, int p, int d)
{
  name = n; power = p; durability = d;
}

我在显示此功能时也遇到了问题...我怎么称呼它?

void Item::Describe() const
{
  cout << name << " (power=" << power << ", durability=" << durability << ")n";
}
Item::~Item()
{
  cout << "** Item " << name << " is being deallocated." << endl;
}
class Warrior
{
private:
  string name;
  int level;
  string profession;
  Item *tool;
public:
  Warrior(string n, int l, string p);
  Warrior(const Warrior& otherObj);
  void GiveTool(string toolName, int toolPower, int toolDurability);
  void Describe() const;
};
Warrior::Warrior(string n, int l, string p)
{
  name = n;
  level = l;
  profession = p;
}
Warrior::Warrior(const Warrior& otherObj)
{
  if(otherObj.tool != NULL)
   this->tool = new Item(*(otherObj.tool));
else
    this->tool = NULL;
}

我认为问题似乎就在这里...这就是我想做的。

如果工具为空,则表示战士没有工具,则给他一个工具。但是,如果他确实有工具,请释放工具变量并给他这个工具。

void Warrior::GiveTool(string toolName, int toolPower, int toolDurability)
{
    if(tool == NULL)
    this->tool = new Item(toolName,toolPower,toolDurability);
    else
 {
    cout << name << "'s existing tool is being replaced." << endl;
    delete tool;
    this->tool = new Item(toolName,toolPower,toolDurability);
  }
}

那么我将如何显示新分配的工具...它会像我在这里所做的那样只是"工具"吗?因为当我运行程序时,它会显示地址而不是内存。

void Warrior::Describe() const
{
  cout << name << " is a level " << level << " " << profession << endl;
  if(tool != NULL)
  {
    cout << "His tool is: ";
    cout <<tool;
    cout << "....";
  }
    else
  {
    cout << "No tooln";
  }
}
int main()
{
    Warrior a("Stephen Curry", 30, "NBA Player");
    a.GiveTool("Basketball", 50, 20);
    a.Describe();
    a.GiveTool("Football", 10, 20);
    a.Describe();
}

我认为输出应该看起来像这样:

斯蒂芬库里是 30 级 NBA 球员

他的工具是:碎屑球

斯蒂芬·库里(Stephen Curry)现有的工具正在被替换。

物品篮球正在解除分配。

斯蒂芬库里是 30 级 NBA 球员

他的工具是:足球

提前谢谢你!任何事情都会有所帮助。我对这个编程很陌生世界,在阅读我的代码时请记住这一点......再次感谢任何帮助谢谢!

您可以使用Item对象而不是指向Item的指针编写程序,并且不会出现您甚至没有出现的大多数问题。

话虽如此,您的代码有几个问题:

  1. 您没有在构造函数的 3 个参数Warrior中初始化toolNULL(更好,nullptr)。
  2. 您没有将Warrior复制构造函数中的所有成员从传入的对象复制到 this
  3. 您缺少 Warrior 的析构函数。
  4. 您缺少 Warrior 的赋值运算符 ( Warrior::operator= )。

对于第一个问题:

Warrior::Warrior(string n, int l, string p) : 
                 name(n), level(l), profession(p), tool(nullptr) {}

请注意成员初始化列表的用法。

第二个问题:

Warrior::Warrior(const Warrior& otherObj) :  
name(otherObj.name), level(otherObj.level), profession(otherObj.profession), tool(nullptr)
{
  if (otherObj.tool)
     tool = new Item(*(otherObj.tool));
}

请注意,所有成员都使用传入对象的成员进行初始化。

第三个问题:

Warrior::~Warror() { delete tool; }

第四个问题:使用复制/交换习惯用法的赋值运算符应如下所示:

#include <algorithm>
//...
Warrior& Warrior::operator=(const Warrior& w)
{
   Warrior temp(w);
   std::swap(temp.tool, tool);
   std::swap(temp.name, name);
   std::swap(temp.level, level);
   std::swap(temp.profession, profession);
   return *this;
} 

总结一下:

  1. 当您有一个包含指向动态分配内存的指针的类,并且您在程序中创建此对象的副本时,您应该遵守规则 3。 由于 Warrior 中缺少赋值运算符和析构函数,因此无法执行此操作。 不遵守此规则将导致程序中出现未定义的行为。
  2. 编写用户定义的复制构造函数时,应复制所有成员,而不仅仅是一个或两个成员。 拥有对象的部分副本是最难诊断的错误之一。 复制所有成员规则的一个主要例外是,如果对象正在被引用计数。
  3. 构造对象时,应将指针成员初始化为某种状态(通常为 nullptr )。

class Warrior中,工具是一个指针 - Item *tool 。因此,使用 * 获取对象。

接下来,为了能够使用 stream 打印类 Item 的对象,您应该重载<<运算符。另一种方法是为该类定义get_name()方法:

const std::string & Item::get_name() const
{
  return name;
}

此函数返回对 name 字段的常量引用,这比返回完整副本更有效,并且可以防止类外可能的修改。同样,该方法是常量,这意味着不能在其中修改任何值。然后,你可以这样写: cout << tool->get_name()

我在显示此功能时也遇到了问题...我怎么称呼它?

tool->Describe();  // From the Warrior class

首先检查空点。