如何定义从虚拟函数继承的静态函数
How to define a static function inherited from a virtual one?
我正在使用C 进行一个小应用程序。我有一个具有虚拟方法的父类。这是标题文件。
class personDB:public person
{
public:
unsigned int id;
public:
personDB();
personDB(QString dbName, QString dbSurname);
personDB(QString dbName, QString dbSurname, unsigned int dbid);
personDB(std::string dbName, std::string dbSurname, unsigned int dbid);
virtual std::string getTableName();
unsigned int getID(void);
void setID(unsigned int myID);
private:
static const std::string tableName;
};
此课程由不同的子类继承。每个子类重新定义 *.cpp文件中分配不同值的tableName
属性。该属性是私人的,由getter返回。作为一个儿童课程的一个示例是标题文件:
#include "persondb.h"
class ScriptBy : public personDB
{
public:
ScriptBy();
ScriptBy(QString dbName, QString dbSurname);
ScriptBy(QString dbName, QString dbSurname, unsigned int dbid);
std::string getTableName();
protected:
static const std::string tableName;
};
我想使函数getTablEname()一个类成员函数,以便它返回同一类的所有实例的相同值,即使没有类的实例,也可以调用。如果我没有错,则应通过将关键字static
放在标题文件中的函数声明之前来完成。但是,当我尝试以这种方式进行编译时,它给了我一个错误,即父类中的函数被称为虚拟。我得到的错误是:
In file included from ../../singlestory.h:4:0,
from ../../volume.h:5,
from ../../dbinterface.h:8,
from ../../dbinterface.cpp:1:
../../scriptby.h:12:24: error: ‘static std::__cxx11::string ScriptBy::getTableName()’ cannot be declared
static std::string getTableName();
^
In file included from ../../dbinterface.h:7:0,
from ../../dbinterface.cpp:1:
../../persondb.h:15:25: error: since ‘virtual std::__cxx11::string personDB::getTableName()’ declared in base class
virtual std::string getTableName();
有没有办法使继承的类成员函数静态?
编辑:如果我不能静态函数,那么如果没有类的实例,我该如何使私有属性在外部访问(并保持私有)?
有没有办法使继承的类成员函数静态?
你不能。您可以将static
函数重命名为其他功能,并从常规成员功能中使用它,如果对您的课程有意义。
class ScriptBy : public personDB
{
virtual std::string getTableName() { return getTableNameStatic(); }
static std::string getTableNameStatic();
};
virtual
和 static
表示2个矛盾的要求。
static
函数是 class-level 构造,并且不受任何特定类实例的约束。
virtual
函数是实例级别构造,因此其行为是由相关对象定义的。
您需要重新考虑您的要求。
我使用了好奇的重复模板模式找到了解决方案。
对我有用的解决方案如下:
文件persondb.h
#ifndef PERSONDB_H
#define PERSONDB_H
#include "person.h"
#include"common.h"
class personDB:public person
{
public:
using person::person;
personDB(QString dbName, QString dbSurname, unsigned int dbid);
personDB(std::string dbName, std::string dbSurname, unsigned int dbid);
unsigned int getID(void);
void setID(unsigned int myID);
protected:
unsigned int id;
};
/* Curiously recurring template paradigm */
template<class Derived>
class PersonDBX: public virtual personDB
{
using personDB::personDB;
protected:
static const std::string tableName;
static const personRelationTable personIDtable;
public:
static std::string static_getTableName(){ return tableName; }
std::string getTableName(){return tableName;}
static std::string static_getIDTableName(){ return personIDtable.tableName; }
std::string getIDTableName(){return personIDtable.tableName;}
static std::string static_getCol1TableID(){ return personIDtable.col1; }
std::string getCol1TableID(){return personIDtable.col1;}
static std::string static_getCol2TableID(){ return personIDtable.col2; }
std::string getCol2TableID(){return personIDtable.col2;}
personDBX(QString dbName, QString dbSurname, unsigned int dbid):personDB(dbName, dbSurname)
{
id = dbid;
}
PersonDBX():personDB(){;}
PersonDBX(QString dbName, QString dbSurname):personDB(dbName, dbSurname){;}
};
template<class Derived> const std::string PersonDBX<Derived>::tableName = "Null";
template<class Derived> const personRelationTable PersonDBX<Derived>::personIDtable = {"Null", "Null", "Null"};
#endif // PERSONDB_H
文件persondb.cpp
#include "persondb.h"
personDB::personDB(QString dbName, QString dbSurname, unsigned int dbid):person(dbName, dbSurname){
id = dbid;
}
personDB::personDB(std::string dbName, std::string dbSurname,
unsigned int dbid):person(dbName, dbSurname){
id = dbid;
}
unsigned int personDB::getID(void){
return id;
}
void personDB::setID(unsigned int myID)
{
id = myID;
}
儿童课是scriptby:
文件scriptby.h
#ifndef SCRIPT_H
#define SCRIPT_H
#include "persondb.h"
class ScriptBy : public PersonDBX<ScriptBy>
{
public:
using PersonDBX::PersonDBX;
ScriptBy(QString dbName, QString dbSurname, unsigned int dbid);
protected:
};
#endif // SCRIPT_H
文件scriptby.cpp
#include "scriptby.h"
#include <iostream>
#include <globals.h>
template<> const std::string PersonDBX<ScriptBy>::tableName = "ScriptBy";
template<> const personRelationTable PersonDBX<ScriptBy>::personIDtable = {"Story_ScriptBy","StoryID", "ScriptByID"};
ScriptBy::ScriptBy(QString dbName, QString dbSurname, unsigned int dbid):PersonDBX(dbName, dbSurname)
{
id = dbid;
}
"是否有一种方法可以使继承的类成员函数静态?" no
相关文章:
- C++无法定义虚拟函数 OUTER 类和头文件
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- 有没有比在库中添加一个并非由所有派生类实现的新虚拟函数更好的设计实践
- 重载 -> shared_ptr 个实例中的箭头运算符<interface>,接口中没有纯虚拟析构函数
- 当覆盖存在时调用基本虚拟"binded to object"函数
- 如何在C++中伪造虚拟可变参数函数模板?
- 类型擦除的std::function与虚拟函数调用的开销
- 重写虚拟函数和继承
- 是否可以使用函数指针调用虚拟析构函数?
- 在没有动态内存的世界中,我是否需要虚拟析构函数?
- 虚拟继承基构造函数消除
- "虚拟""覆盖"析构函数
- 类中的虚拟布尔函数参数不起作用
- 用纯虚拟函数兜圈子
- 将C++子类成员函数(虚拟实现)传递给 C 类型函数指针
- 尝试在 QLabel 上绘画失败(无法在没有对象的情况下调用成员函数"虚拟无效 QLabel::p aintEvent(QPaintEvent*)")
- 声明析构函数虚拟就足够了吗?
- 视觉 C++当我们在基类中使函数成为纯虚拟时,那么在子类中再次使相同的函数虚拟的必要性是什么
- 重载函数(虚拟/非虚拟)