C++ - 我可以传递子类作为参数而不是基类吗

发布于 2025-01-11 15:42:50 字数 1124 浏览 0 评论 0原文

我有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2025-01-18 15:42:50

尽管 WorldDatabaseConnectionMySQLConnection 的子类,但模板类 PreparedStatement 不是 PreparedStatement的子类;

Even though WorldDatabaseConnection is a child class of MySQLConnection, the template class PreparedStatement<WorldDatabaseConnection> is not a child class of PreparedStatement<MySQLConnection>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文