C++ - 我可以传递子类作为参数而不是基类吗
我有这个:
class DATABASE_API MySQLConnection
{
}
然后是一个子类:
class DATABASE_API WorldDatabaseConnection : public MySQLConnection
{
}
然后我有这个:
class GameDatabase {
public:
GameDatabase(PreparedStatement<MySQLConnection>* preparedStatement, GameDatabaseFlags gdf)
{
_gameObjectDatabaseFlag = gdf;
_preparedStatement = preparedStatement;
};
GameDatabaseFlags _gameObjectDatabaseFlag;
protected:
uint32_t versionId;
virtual void MapGameDatabase(GameDatabaseContainer gameDatabaseContainer) = 0;
PreparedStatement<MySQLConnection>* _preparedStatement;
};
当我尝试像这样初始化 GameDatabase
时:
PreparedStatement<WorldDatabaseConnection> * stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_GAMEOBJECTS_TEMPLATE);
auto gameDatabase = new GameDatabase(stmt,GAMEOBJECTS_DB);
我收到以下错误:
没有匹配的构造函数来初始化“GameDatabase”
这是为什么?我不能简单地使用子类WorldDatabaseConnection
来代替基类MySQLConnection
吗?
I have this:
class DATABASE_API MySQLConnection
{
}
And then a child class:
class DATABASE_API WorldDatabaseConnection : public MySQLConnection
{
}
Then I have this:
class GameDatabase {
public:
GameDatabase(PreparedStatement<MySQLConnection>* preparedStatement, GameDatabaseFlags gdf)
{
_gameObjectDatabaseFlag = gdf;
_preparedStatement = preparedStatement;
};
GameDatabaseFlags _gameObjectDatabaseFlag;
protected:
uint32_t versionId;
virtual void MapGameDatabase(GameDatabaseContainer gameDatabaseContainer) = 0;
PreparedStatement<MySQLConnection>* _preparedStatement;
};
When I try to initialize GameDatabase
like so:
PreparedStatement<WorldDatabaseConnection> * stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_GAMEOBJECTS_TEMPLATE);
auto gameDatabase = new GameDatabase(stmt,GAMEOBJECTS_DB);
I get the following error:
No matching constructor for initialization of 'GameDatabase'
Why is that? Can't I simple use the child class WorldDatabaseConnection
in place of the base class MySQLConnection
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管
WorldDatabaseConnection
是MySQLConnection
的子类,但模板类PreparedStatement
不是PreparedStatement的子类;
。Even though
WorldDatabaseConnection
is a child class ofMySQLConnection
, the template classPreparedStatement<WorldDatabaseConnection>
is not a child class ofPreparedStatement<MySQLConnection>
.