如有任何帮助,我将不胜感激。
我有一个简单的容器模板类数据集。
当实例化数据集的数据集时,还有一个专门化允许不同的实现。
由于 Datasets 的 Dataset 将是一个异构容器,因此有一个基本抽象类 List 必须为每个 Dataset 声明一个公共(public)接口(interface)。那就是 get()。
问题是:我无法在 List::get() 中指定返回类型,所以我需要覆盖后代中的 void* 返回。
我阅读了以下协方差限制:
•The function B::f returns a reference or pointer to a class of type T, and A::f returns > a pointer or a reference to an unambiguous direct or indirect base class of T.
•The const or volatile qualification of the pointer or reference returned by B::f has the same or less const or volatile qualification of the pointer or reference returned by A::f.
•The return type of B::f must be complete at the point of declaration of B::f, or it can be of type B.
好吧,让包含的对象从同一个基类派生对我来说没有用。
那么,有解决方法吗?更好的是:执行此操作的正确 C++ 方法是什么?
如果您想知道,我正在尝试做的是一个“关系容器”,可以这么说。但是无论如何你都不会在下面的代码中看到关系部分。
目标是存储数据,使容器不知道列表之间的任何关系。
然后我会将关系存储在一个列表中。例如:
人员名单
0 约翰
1 亚瑟
2卡罗琳
汽车 list
0 - 福特
1 - 大众汽车
2 - 克莱斯勒
关系列表
0 - 1;2
1 - 0;1
2 - 0;2
与关系数据库非常相似。 此外,我还有一些小问题,例如让 set() 接受 maxsize 个参数。无法使用 va_list 实现此目的。无论如何,也许我匆忙完成了这个。
最后一点:为了学习,我故意避免使用 STL,但如果已经有这样的功能可用,我想知道。
非常感谢!
下面的代码:
#include <iostream>
using namespace std;
//--------------------------------------------------//
// TRIVIAL DUMMY CLASSES
class Person
{
public:
Person(char* n, unsigned int a): _name(n), _age(a){}
char* name(){cout << "Im " << _name << endl; return _name;}
unsigned int age(){cout << "Im " << _age << " years old." << endl; return _age;}
private:
char* _name;
unsigned int _age;
} p1("john", 28), p2("Arthur", 26), p3("Caoline", 31);
class Car
{
public:
Car(char* m, unsigned int y): _model(m), _year(y){}
char* model(){cout << "Its a " << _model << endl; return _model;}
unsigned int year(){cout << "Im" << _year << " years old." << endl; return _year;}
private:
char* _model;
unsigned int _year;
} c1("Chrysler C-300", 1955), c2("Chrysler VH Charger", 1971), c3("Ford Fairlane", 1960);
//--------------------------------------------------//
class List
{
public:
List(): length(0){}
// common interface so Lists of Lists can be created
// can´t specify a return type so it returns void*
// BUT need to override it in descendants. WHY IT DOESN´T WORK?
virtual void* get(unsigned int i) = 0;
protected:
unsigned int length;
};
//--------------------------------------------------//
template <class C, unsigned int maxsize>
class Dataset: public List
{
public:
Dataset()
{
// initialize pointers to null
for(int i = 0; i < maxsize; i++){
data[i] = 0;
}
};
// C* return type is ignored
C* get(unsigned int i)
{return data[i];};
int set(C* dataIn)
{
data[length] = dataIn;
return length++;
};
protected:
C* data[maxsize];
};
template <unsigned int maxsize>
class Dataset <List, maxsize>: public List
{
public:
Dataset()
{
// initialize pointers to null
for(int i = 0; i < maxsize; i++){
data[i] = 0;
}
};
List* get(unsigned int i)
{return data[i];};
int set(List* dataIn)
{
data[length] = dataIn;
return length++;
};
protected:
List* data[maxsize];
};
//--------------------------------------------------//
int main()
{
Dataset <Person, 3> columnPerson;
// populate person list
columnPerson.set(&p1);
columnPerson.set(&p2);
columnPerson.set(&p3);
Dataset <Car, 3> columnCar;
// populate car list
columnCar.set(&c1);
columnCar.set(&c2);
columnCar.set(&c3);
Dataset <List, 10> relations; // create a list of lists
// populate it
relations.set(&columnPerson);
relations.set(&columnCar);
// getting a void* and casting it
Person* ptrPerson = (Person*) relations.get(0)->get(0);
ptrPerson->name();
int i;
cin >> i;
};
最佳答案
我不会尝试回答实现细节,但您可以按照您的要求从 boost.any 中获取功能图书馆。 heterogenous containers上有教程使用它。
关于c++ - C++ 中的关系容器和多态性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3791408/