在 C++ 中对 class 实例使用 extern
Using extern for a class instance in C++
我想要我的引擎的全局实例 class 以便在创建演员实例时可以隐式地将演员添加到我的演员管理器中。为简单起见,我将所有代码归结为一个文件。
我的问题是使用 extern,因为在我还没有定义我的 class 时我如何使用 extern。我想也许 c++ 会允许我使用父作为类型,但运气不好。
#include <iostream>
namespace Engine { class Abstract {};};
extern Engine::Abstract *engine;
namespace Engine { class Actor {
public:
Actor(){
engine->actors->add(this);
}
};};
namespace Engine { class ActorManager {
public:
void add(){
std::cout << "Actor.." << std::endl;
}
};};
namespace Engine { class Base : Abstract {
public:
ActorManager *actors;
void run() {
actors = new ActorManager();
this->setup();
}
virtual void setup(){}
};};
class Player : Engine::Actor {};
class MyGame : public Engine::Base {
void setup(){
Player *player;
player = new Player();
}
};
MyGame *engine = new MyGame;
int main(int argc, const char ** argv)
{
engine->run();
delete engine;
return 0;
}
这就是我在 ruby 中实现的方法
$game = nil
module Engine
class Actor
def initialize
$game.actors.add
end
end
class ActorManager
def add
puts 'adding an actor'
end
end
class Base
attr_accessor :actors
def initialize
self.actors = ActorManager.new
end
end
end
class Player < Engine::Actor
end
class Game < Engine::Base
def run
Player.new
end
end
$game = Game.new
$game.run
header 仅适用于大多数基础 类 因此它可以编译但不会 link(如您所知)
namespace Engine {
// header info only - nothing is defined here
class Actor {
};
// interface for ActorManager
class ActorManager {
public:
void add(const Actor*);
};
extern ActorManager * global_pManager;
// interface for Game
class Game : public ActorManager {
public:
virtual ~Game(){}; // you need a virtual destructor
virtual void setup() = 0; // abstract interface
virtual void run() = 0; // abstract interface
};
};
// header for your game and player logic
class Player : public Engine::Actor {
};
class MyGame : public Engine::Game {
public:
void setup();
void run();
};
int main(int argc, const char ** argv)
{
// scope of main is a better way to control the lifetime of this object
MyGame engine;
// here's the global pointer you requested
Engine::global_pManager = static_cast<Engine::ActorManager*>(&engine);
engine.run();
return 0;
}
我想要我的引擎的全局实例 class 以便在创建演员实例时可以隐式地将演员添加到我的演员管理器中。为简单起见,我将所有代码归结为一个文件。
我的问题是使用 extern,因为在我还没有定义我的 class 时我如何使用 extern。我想也许 c++ 会允许我使用父作为类型,但运气不好。
#include <iostream>
namespace Engine { class Abstract {};};
extern Engine::Abstract *engine;
namespace Engine { class Actor {
public:
Actor(){
engine->actors->add(this);
}
};};
namespace Engine { class ActorManager {
public:
void add(){
std::cout << "Actor.." << std::endl;
}
};};
namespace Engine { class Base : Abstract {
public:
ActorManager *actors;
void run() {
actors = new ActorManager();
this->setup();
}
virtual void setup(){}
};};
class Player : Engine::Actor {};
class MyGame : public Engine::Base {
void setup(){
Player *player;
player = new Player();
}
};
MyGame *engine = new MyGame;
int main(int argc, const char ** argv)
{
engine->run();
delete engine;
return 0;
}
这就是我在 ruby 中实现的方法
$game = nil
module Engine
class Actor
def initialize
$game.actors.add
end
end
class ActorManager
def add
puts 'adding an actor'
end
end
class Base
attr_accessor :actors
def initialize
self.actors = ActorManager.new
end
end
end
class Player < Engine::Actor
end
class Game < Engine::Base
def run
Player.new
end
end
$game = Game.new
$game.run
header 仅适用于大多数基础 类 因此它可以编译但不会 link(如您所知)
namespace Engine {
// header info only - nothing is defined here
class Actor {
};
// interface for ActorManager
class ActorManager {
public:
void add(const Actor*);
};
extern ActorManager * global_pManager;
// interface for Game
class Game : public ActorManager {
public:
virtual ~Game(){}; // you need a virtual destructor
virtual void setup() = 0; // abstract interface
virtual void run() = 0; // abstract interface
};
};
// header for your game and player logic
class Player : public Engine::Actor {
};
class MyGame : public Engine::Game {
public:
void setup();
void run();
};
int main(int argc, const char ** argv)
{
// scope of main is a better way to control the lifetime of this object
MyGame engine;
// here's the global pointer you requested
Engine::global_pManager = static_cast<Engine::ActorManager*>(&engine);
engine.run();
return 0;
}