指针问题?这个问题?类问题
Pointer issue? This issue? class issue?
#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
的指针编写程序,并且不会出现您甚至没有出现的大多数问题。
话虽如此,您的代码有几个问题:
- 您没有在构造函数的 3 个参数
Warrior
中初始化tool
以NULL
(更好,nullptr
)。 - 您没有将
Warrior
复制构造函数中的所有成员从传入的对象复制到 this。 - 您缺少
Warrior
的析构函数。 - 您缺少
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;
}
总结一下:
- 当您有一个包含指向动态分配内存的指针的类,并且您在程序中创建此对象的副本时,您应该遵守规则 3。 由于
Warrior
中缺少赋值运算符和析构函数,因此无法执行此操作。 不遵守此规则将导致程序中出现未定义的行为。 - 编写用户定义的复制构造函数时,应复制所有成员,而不仅仅是一个或两个成员。 拥有对象的部分副本是最难诊断的错误之一。 复制所有成员规则的一个主要例外是,如果对象正在被引用计数。
- 构造对象时,应将指针成员初始化为某种状态(通常为
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
首先检查空点。
- 关于 c++ 函数中指针赋值的简单问题
- 链表指针问题
- C++ 关于指针取消引用的技术问题
- C++中的指针和常量问题不大
- 包含矢量指针的结构的内存释放问题
- 指针问题:从不兼容的类型"int"分配给"int *"
- 将字节数组转换为带有字节序问题的指针
- 关于如何使用指向主窗口的指针的 QT 问题
- 当成员值从指针更改为非指针时,C++常量问题
- 为什么循环会导致指针出现问题?
- 使用指针计算堆栈问题的大 O 表示法
- 构造函数 (C++) 中的 char 指针参数存在问题
- 指向包含对齐 C 结构C++类的 C 指针的对齐问题
- 涉及指针和手动实现的矩阵类的问题
- 从基指针到派生的强制转换问题
- 迭代器的指针操作问题
- 64 位迁移问题:指针更改
- C++模板使用问题指针
- 跳到C++第13章练习问题4-指针
- 用std::pair数组初始化std::map问题(指针错误?)