将指向类的指针从一个类更改为另一个类 我对C++很陌生,并且很难把指针点从一个类变成另一个类。这就是我所拥有的,它编译没有错误,但没有按照我想要的方式工作 JungleMap *Map; class JungleMap { public: void goNorth() { cout << "You are going north towards the river.\n"; delete[] Map; RiverMap *Map; } } class RiverMap { public: void goNorth() { cout << "You are going north away from the river.\n"; delete[] Map; JungleMap *Map; } } int main() { Map->goNorth(); Map->goNorth(); }
这就是我想要的输出:将指向类的指针从一个类更改为另一个类 我对C++很陌生,并且很难把指针点从一个类变成另一个类。这就是我所拥有的,它编译没有错误,但没有按照我想要的方式工作 JungleMap *Map; class JungleMap { public: void goNorth() { cout << "You are going north towards the river.\n"; delete[] Map; RiverMap *Map; } } class RiverMap { public: void goNorth() { cout << "You are going north away from the river.\n"; delete[] Map; JungleMap *Map; } } int main() { Map->goNorth(); Map->goNorth(); },c++,class,pointers,delete-operator,C++,Class,Pointers,Delete Operator,这就是我想要的输出: You are going north towards the river. You are going north away from the river. 我如何做到这一点?它真的让我很烦,尤其是因为它编译起来没有问题。仅仅创建JungleMap*并不能创建JungleMap。你形成了一个指针,但没有指向任何地方 这尤其危险,因为您随后会取消对它的引用,然后尝试通过它进行删除。是的,这是可以编译的,因为编译器无法在一般情况下诊断这一点(而且永远不需要尝试),但在运行时
You are going north towards the river.
You are going north away from the river.
我如何做到这一点?它真的让我很烦,尤其是因为它编译起来没有问题。仅仅创建
JungleMap*
并不能创建JungleMap
。你形成了一个指针,但没有指向任何地方
这尤其危险,因为您随后会取消对它的引用,然后尝试通过它进行删除。是的,这是可以编译的,因为编译器无法在一般情况下诊断这一点(而且永远不需要尝试),但在运行时,您将获得从无声的虚无、崩溃到核爆炸的一切
您还试图在两个不同的类中调用不同的函数,通过更改指针的类型(没有任何继承),这是不可能的,并且会阻止代码编译,即使您试图通过在本地重新声明变量来绕过它。我可以列举大量的误解,但足以说明是时候阅读了
如果我知道您想要实现什么,我会建议将继承和动态分配相结合。一个常见的错误是提供无意义的代码,然后期望我们从无意义的代码中知道您的目标是什么;不幸的是,我们对C++编译器所做的事情有着同样的想法。p> 仅仅创建一个
JungleMap*
并不能创建一个JungleMap
。你形成了一个指针,但没有指向任何地方
这尤其危险,因为您随后会取消对它的引用,然后尝试通过它进行删除。是的,这是可以编译的,因为编译器无法在一般情况下诊断这一点(而且永远不需要尝试),但在运行时,您将获得从无声的虚无、崩溃到核爆炸的一切
您还试图在两个不同的类中调用不同的函数,通过更改指针的类型(没有任何继承),这是不可能的,并且会阻止代码编译,即使您试图通过在本地重新声明变量来绕过它。我可以列举大量的误解,但足以说明是时候阅读了
如果我知道您想要实现什么,我会建议将继承和动态分配相结合。一个常见的错误是提供无意义的代码,然后期望我们从无意义的代码中知道您的目标是什么;不幸的是,我们对C++编译器所做的事情有着同样的想法。p> 仅仅创建一个
JungleMap*
并不能创建一个JungleMap
。你形成了一个指针,但没有指向任何地方
这尤其危险,因为您随后会取消对它的引用,然后尝试通过它进行删除。是的,这是可以编译的,因为编译器无法在一般情况下诊断这一点(而且永远不需要尝试),但在运行时,您将获得从无声的虚无、崩溃到核爆炸的一切
您还试图在两个不同的类中调用不同的函数,通过更改指针的类型(没有任何继承),这是不可能的,并且会阻止代码编译,即使您试图通过在本地重新声明变量来绕过它。我可以列举大量的误解,但足以说明是时候阅读了
如果我知道您想要实现什么,我会建议将继承和动态分配相结合。一个常见的错误是提供无意义的代码,然后期望我们从无意义的代码中知道您的目标是什么;不幸的是,我们对C++编译器所做的事情有着同样的想法。p> 仅仅创建一个
JungleMap*
并不能创建一个JungleMap
。你形成了一个指针,但没有指向任何地方
这尤其危险,因为您随后会取消对它的引用,然后尝试通过它进行删除。是的,这是可以编译的,因为编译器无法在一般情况下诊断这一点(而且永远不需要尝试),但在运行时,您将获得从无声的虚无、崩溃到核爆炸的一切
您还试图在两个不同的类中调用不同的函数,通过更改指针的类型(没有任何继承),这是不可能的,并且会阻止代码编译,即使您试图通过在本地重新声明变量来绕过它。我可以列举大量的误解,但足以说明是时候阅读了
如果我知道您想要实现什么,我会建议将继承和动态分配相结合。一个常见的错误是提供无意义的代码,然后期望我们从无意义的代码中知道您的目标是什么;不幸的是,我们对C++编译器所做的事情有着同样的想法。p> 你应该坐下来思考你要解决的问题,然后做出正确的设计。在您的情况下,您有两个“位置”,并且“玩家”应该能够在这些位置之间移动。从这一点开始,我们确定了两个可能的类别(
Location
和Player
)和一个行为(Player可以从一个位置移动到另一个位置)
根据以上信息,您可以执行以下操作:
class Location
{
public:
void setNorth(Location* loc)
{
north_ = loc;
}
Location* getNorth() const
{
return north_;
}
void setSouth(Location* loc)
{
south_ = loc;
}
Location* getSouth() const
{
return south_;
}
void setDescription(const std::string& descr)
{
description_ = descr;
}
const std::string& getDescription() const
{
return description_;
}
protected:
Location() {} // Made protected to prevent direct creation of Location instances
private:
Location* north_;
Location* south_;
std::string description_;
};
class Jungle : public Location
{
public:
Jungle() : Location()
{
setDescription("You are in a jungle.");
}
};
class River : public Location
{
public:
River() : Location()
{
setDescription("You are close to a river.");
}
};
// The actual "map"
std::vector<Location*> map
void createMap()
{
map.push_back(new Jungle);
map.push_back(new River);
map[0]->setNorth(map[1]);
map[1]->setSouth(map[0]);
}
class Player
{
public:
Player(Location* initialLocation)
: currentLocation_(initialLocation)
{
std::cout << currentLocation_->getDescription() << '\n';
}
...
// Other methods and members needed for a "player"
void goNorth()
{
if (currentLocation_ && currentLocation_->getNorth())
{
currentLocation_ = currentLocation_->getNorth();
std::cout << currentLocation_->getDescription() << '\n';
}
}
void goSouth()
{
if (currentLocation_ && currentLocation_->getSouth())
{
currentLocation_ = currentLocation_->getSouth();
std::cout << currentLocation_->getDescription() << '\n';
}
}
private:
Location* currentLocation_; // The players current location
};
int main()
{
createMap(); // Create the "map"
Player player(map[0]); // Create a player and place "him" in the jungle
// Move the player around a little
player.goNorth();
player.goSouth();
}
类位置
{
公众:
无效设置北(位置*loc)
{
北=loc;
}
位置*getNorth()常量
{
返回北方;
}
空设置出口(位置*loc)
{
南部=loc;
}
位置*getSouth()常量
{
返回南方;
}
void setDescription(const std::string和descr)
{
描述=描述;
}
常量std::string和getDescript
class Location
{
public:
void setNorth(Location* loc)
{
north_ = loc;
}
Location* getNorth() const
{
return north_;
}
void setSouth(Location* loc)
{
south_ = loc;
}
Location* getSouth() const
{
return south_;
}
void setDescription(const std::string& descr)
{
description_ = descr;
}
const std::string& getDescription() const
{
return description_;
}
protected:
Location() {} // Made protected to prevent direct creation of Location instances
private:
Location* north_;
Location* south_;
std::string description_;
};
class Jungle : public Location
{
public:
Jungle() : Location()
{
setDescription("You are in a jungle.");
}
};
class River : public Location
{
public:
River() : Location()
{
setDescription("You are close to a river.");
}
};
// The actual "map"
std::vector<Location*> map
void createMap()
{
map.push_back(new Jungle);
map.push_back(new River);
map[0]->setNorth(map[1]);
map[1]->setSouth(map[0]);
}
class Player
{
public:
Player(Location* initialLocation)
: currentLocation_(initialLocation)
{
std::cout << currentLocation_->getDescription() << '\n';
}
...
// Other methods and members needed for a "player"
void goNorth()
{
if (currentLocation_ && currentLocation_->getNorth())
{
currentLocation_ = currentLocation_->getNorth();
std::cout << currentLocation_->getDescription() << '\n';
}
}
void goSouth()
{
if (currentLocation_ && currentLocation_->getSouth())
{
currentLocation_ = currentLocation_->getSouth();
std::cout << currentLocation_->getDescription() << '\n';
}
}
private:
Location* currentLocation_; // The players current location
};
int main()
{
createMap(); // Create the "map"
Player player(map[0]); // Create a player and place "him" in the jungle
// Move the player around a little
player.goNorth();
player.goSouth();
}
class Map {
public:
virtual void goNorth() { cout<<"Sorry, you can't go that way"; }
virtual void goSouth() { cout<<"Sorry, you can't go that way"; }
};
Map *map;
class RiverMap;
class JungleMap : public Map {
public:
void goNorth();
};
class RiverMap : public Map {
public:
void goSouth();
};
void JungleMap::goNorth() {
cout<<"You are going north towards the river.\n";
delete map;
map=new RiverMap;
}
void RiverMap::goSouth() {
cout<<"You are going south towards the jungle.\n";
delete map;
map=new JungleMap;
}
JungleMap *Map;
struct Foo {};
struct Baz { Baz() { std::cout << "Baz is here\n"; } };
struct Foo {};
struct Bar { Bar() { std::cout << "Bar is here\n"; } };
struct Baz {};
int main() {
int i; // no side effects, i is trivial.
char* p; // no side effects, p is a pointer (trivial) type
std::string* sp; // trivial, pointer
Foo f; // trivial
Bar b; // non-trivial, baz has a user-defined ctor that has side-effects.
Bar* bar; // trivial, unassigned pointer type.
Bar* bar2 = new Bar(); // side effects.
Bar bar(); // syntax error, "the most vexing parse"
}
struct Foo {};
struct Bar { Bar() { std::cout << "Bar is here\n"; } };
int main() {
Bar* bar2 = new Bar(); // side effects.
Bar bar(); // syntax error, "the most vexing parse"
}
#include <iostream>
int main() {
int i = 1;
if (i == 1) {
float i = 3.141;
std::cout << "inner i = " << i << '\n';
}
std::cout << "outer i = " << i << '\n';
return 0;
}