我具有如下面的實施2簡單的C++頭:C++無法訪問抽象類的向量元素?
Attribute.h
#include <string>
using namespace std;
class IAttribute
{
virtual string getName(){};
};
class StringAttribute : public IAttribute
{
private:
string name = "";
string value = "";
public:
StringAttribute(string name, string value)
{
this->name = name;
this->value = value;
}
string getName()
{
return this->name;
}
string getStrValue()
{
return value;
}
void setValue(string value)
{
this->value = value;
}
};
tableRow.h
#include "attribute.h"
#include <vector>
using namespace std;
class TableRow
{
private:
vector<IAttribute *> attributeList;
int rowId;
public:
TableRow(int rowId)
{
this->rowId = rowId;
}
void addStrAttribute(string name, string value)
{
attributeList.push_back(new StringAttribute(name, value));
}
StringAttribute getStrAtt(string name)
{
for (int i = 0; i < (int)attributeList.size(); i++)
{
if (attributeList[i]->)//couldn't access the methods of StringAttributeImp
{
}
}
}
};
如的TableRow報頭的上面的評論,我不能」訪問實現類的方法和屬性。哪裏不對?
即使您將getName()公開,代碼也不會被編譯。例如,getName()不是抽象的,它具有不返回字符串的實現。 IAttribute不具有虛擬析構函數。注意代碼中的這些問題。 – Jagannath 2014-10-30 22:37:37