我开始学习C ++,并且来自C#背景,我遇到很多问题。
我想做的是尝试使用C ++复制与以下C#代码段中所做的完全相同的事情。这只是MVP模式的简单实现,我经常使用它。
我尝试了许多不同的方法来使之与适当的现代C ++一起使用,但是它不断给我带来我不理解的编译错误。它们中的大多数是由于以下事实:我无法找到将接口作为构造函数参数传递以及将接口存储为类的字段的正确方法。 有人可以将此C#代码转换为适当的C ++代码,还是至少请给我一些建议?预先感谢。
注意:我正在尝试仅使用类声明创建头文件,并使用实际实现制作cpp文件。
// This is my C# implementation that I want to convert in C++
public class Program
{
public static void Main()
{
IGameView view = new GameView();
Game game = new Game(view);
view.SetPresenter(game);
}
}
public interface IGameView
{
void SetPresenter(Game game);
}
public class GameView : IGameView
{
public void SetPresenter(Game game)
{
_game = game;
}
Game _game;
}
public class Game
{
public Game(IGameView view)
{
_view = view;
}
IGameView _view;
}
这是我试图进行编译的C ++代码。 为了清楚和简洁起见,我将所有内容都没有.h和.cpp放在这里,但是正如我所说的,我实际上是在将类与实现分开。
class Game
{
public:
(IGameView& view) : _view(view)
{ }
Game operator=(const Game &);
private:
IGameView& _view;
};
class IGameView
{
public:
virtual ~IGameView() {}
virtual void SetPresenter(const Game) = 0;
};
class GameView : public IGameView
{
public:
GameView();
void SetPresenter(const Game game) override
{
_game = game;
}
private:
Game& _game;
};
int main()
{
IGameView view;
Game game(view);
view.SetPresenter(game);
}
答案 0 :(得分:2)
elgonzo是对的。您不应该通过翻译来开始学习语言,尤其是。介于C#和C ++之间。它们之间唯一的相似之处是关键字的命名约定。
在这种情况下,重要的区别(除了如何在C ++中声明接口外)是C ++中的所有类型均按值保存,而C#类则按引用保存。您无法使用任何一种语言创建接口的实例(即,您不能在C#中创建new IGameView()
)。
因此,您的Game
类型不能按值保存接口类型。它必须是指针或引用。这与C#完全不同,我建议您像其他评论者所说的那样做,先学习C ++基础知识,然后再回到此。
编辑:
这是您发布的C ++代码的一种有效形式。它带有注释,解释了为什么/何时去做它。
// C++ requires declaring types before you use them.
// if we want to use Game before defining it we must at least declare that it exists.
class Game;
// this is an interface because it contains abstract (pure virtual) functions.
// you cannot create an instance of an abstract type - but you can make a reference or pointer to one.
struct IGameView
{
// we might want polymorphic deletion - so to be safe we'll make a virtual dtor.
virtual ~IGameView() {};
// Game is probably expensive to copy - pass it by reference.
// = 0 makes this an abstract method, which makes this an abstract type.
virtual void SetPresenter(Game &game) = 0;
};
// --------------------------------------------
class Game
{
public:
// take a reference to the (interface) object to use (we can't pass an abstract type by value)
Game(IGameView &view) : _view(view) { }
private:
// hold a reference to the IGameView (interface) object.
// if you ever wanted this to refer to something else this would need to be pointer instead.
// references are kind of like pointers that cannot be repointed to something else.
IGameView &_view;
};
class GameView : public IGameView
{
public:
GameView();
virtual void SetPresenter(Game &game) override
{
_game = &game;
}
private:
// hold a pointer to the Game object.
// this has to be a pointer because SetPresenter() needs to be able to repoint it (refences can't do that).
// for safety, initialize this to null.
Game *_game = nullptr;
};
// ---------------------------------------------
int main()
{
GameView view; // create the game view to use
Game game(view); // create the game object and use that view
view.SetPresenter(game); // set the view to use the game object
return 0;
}