问题描述
我有一个问题,通过包含2个虚拟函数的接口从派生类访问基类成员变量。主要目的是我需要在两个不同的程序阶段使用这些类。
第一阶段是通过ArrayStemmedSnippet类来填充数组文本片段/句子
第二个阶段,在程序的稍后一点,是从基础中定义的数组/片段数组中获取项目类通过SuffixableElement接口类。
我有强烈的感觉,我在这里缺少一些基础。我认为有一个基本的设计或逻辑错误。我也不得不承认,我不是一个C ++亲,意味着我仍然是一个C ++初学者,所以请与我一起承担....
让我告诉你代码我有:
类定义:
using namespace std;
// ---类表示一个StemmedSentence对象的词干术语
class StemmedTerm {
private:
string _word;
string _stemmed;
public:
StemmedTerm(string word,string stemmed);
〜StemmedTerm();
// ---实现StemmedTerm接口
string getTerm();
string getStemmed();
};
class StemmedSentence;
class SuffixableElement;
// ---包含句子句子的代码片段
class ArrayStemmedSnippet {
friend class StemmedSentence;
public:
ArrayStemmedSnippet();
ArrayStemmedSnippet(Array snippetTerms);
〜ArrayStemmedSnippet();
SuffixableElement * getSentence(int n);
int size()const;
private:
数组片段;
数组句
};
// ---从ArrayStemmedSnippet中提取的句子
class StemmedSentence:public ArrayStemmedSnippet {
public:
StemmedSentence(ArrayStemmedSnippet * p);
StemmedSentence(const int start,const int end);
virtual〜StemmedSentence();
virtual void * get(int index)const;
virtual int size()const;
private:
int _start;
int _end;
ArrayStemmedSnippet * parent;
};
// ---访问句柄和其大小的接口
class SuffixableElement {
public:
virtual〜SuffixableElement()= 0;
virtual void * get(int index)const = 0;
virtual int size()const = 0;
};
实施:
使用命名空间std;
StemmedSentence :: StemmedSentence(const int start,const int end)
: _start(start),_end(end){
}
StemmedSentence :: StemmedSentence(ArrayStemmedSnippet * p)
:parent(p){
}
StemmedSentence 〜StemmedSentence(){
}
// ---实现接口SuffixableElement
void * StemmedSentence :: get(int index)const {
if(index == ) - 1)){
return NULL; // ---结束字符串(句子)
}
//从ArrayStemmedSnippet中的snippets数组获取词干术语类
//为null,因为数组不可访问通过后缀iFace / eg not defined
return snippet [_start + index];
}
// ---实现接口SuffixableElement
int StemmedSentence :: size()const {
return _end - _start + 2;
}
// ---将句子数组添加到句子中。 NULL代表句末
ArrayStemmedSnippet :: ArrayStemmedSnippet(){}
ArrayStemmedSnippet :: ArrayStemmedSnippet(Array snippetTerms)
:snippet(snippetTerms){
int index = 0;
for(int i = 0; i< snippetTerms.getLength(); i ++){
if(snippetTerms [i] == NULL){
sentence.append(new StemmedSentence ,i-1));
index = i + 1;
}
}
}
ArrayStemmedSnippet ::〜ArrayStemmedSnippet(){
for(int i = 0; i< sentence.getLength(); i ++)
delete sentence [i];
}
}
int ArrayStemmedSnippet :: size()const {
return sentence.getLength();
}
// ---返回此片段的第n句
SuffixableElement * ArrayStemmedSnippet :: getSentence(int n){
StemmedSentence(this); // --- just a try:try传递基础实例到派生类
return(SuffixableElement *)sentence [n];
}
主体:
int main(){
//句子1:
数组被阻止;
StemmedTerm * st1 = new StemmedTerm(Mouse,Mouse);
StemmedTerm * st2 = new StemmedTerm(ate,ate);
StemmedTerm * st3 = new StemmedTerm(cheese,cheese);
stemmed.append(st1);
stemmed.append(st2);
stemmed.append(st3);
stemmed.append(NULL); // ---- end of snippet sentence
//句子2:
StemmedTerm * st21 = new StemmedTerm(Cat,Cat);
StemmedTerm * st22 = new StemmedTerm(ate,ate);
StemmedTerm * st23 = new StemmedTerm(cheese,cheese);
StemmedTerm * st24 = new StemmedTerm(too,too);
stemmed.append(st21);
stemmed.append(st22);
stemmed.append(st23);
stemmed.append(st24);
stemmed.append(NULL); // ----结束片断句子
// ok - 一个stemmedsnippet与2个句子
ArrayStemmedSnippet屁股(stemmed);
//做一些其他的东西...稍后在程序中....
// ---通过SuffixableElements接口获取元素和大小信息
SuffixableElement * currentElement =(SuffixableElement *)ass.getSentence(1);
cout<< cur。element size =< currentElement-> size()<< endl;
StemmedTerm * st =(StemmedTerm *)currentElement-> get(2,ass);
string str = st-> toString();
cout< 第二个字是=< str
delete st1;
delete st2;
delete st3;
delete st21;
delete st22;
delete st23;
delete st24;
return 1;
}
我从上面跳过了一些函数(toString()和其他不相关的fct)保持
代码尽可能短。
所以主要的问题是当我调用
SuffixableElement * currentElement =(SuffixableElement *)ass.getSentence(1);
- 这工作正常!
cout< cur。element size =< currentElement-> size()<< endl;
// ---不工作,因为基类的片段数组没有通过
// ---接口... 。
// ---也使用
// --- getSenctence()Fct传递基类ArrayStemmedSnippet的实例没有帮助。
StemmedTerm * st =(StemmedTerm *)currentElement-> get(2); //应该返回cheese
string str = st-> toString();
cout< str =< str<< endl;
我需要知道的是,通过get索引)使用SuffixableElement接口调用
。 size()Fct没有问题,因为我实现了派生类并访问本地成员_start / _end。
同样StemmedSentence类作为ArrayStemmedSnippet中的嵌套类不工作,不像Java(我想)C ++不让我访问ArrayStemmedSnippet的对象像这样。我相信我有一些基本的误解。
我缺少什么?
任何帮助都非常感谢!!
// ---返回此片段的第n句
SuffixableElement * ArrayStemmedSnippet :: getSentence(int n){
// ...
return(SuffixableElement *)sentence [n];
}
'sentence'是StemmedSentence类的对象数组。 StemmedSentence不是从SuffixableElement派生的,也就是它不提供该接口。
请尝试以下操作:
class StemmedSentence:public ArrayStemmedSnippet,public SuffixableElement {
// ...
};
和
SuffixableElement * ArrayStemmedSnippet :: getSentence(int n){
StemmedSentence * sentence = sentence [n];
sentence-> setParent(this);
return sentence;
}
I have a problem to access a base class member variable from a derived class thru a interface containing 2 virtual functions. The main purpose is that I need to work with these classes at 2 different programn stages.First stage is to fill an array with text snippets / sentences thru the ArrayStemmedSnippet classThe second stage, at a later point in the program, is getting items from the sentences array/snippet array defined in the base class thru the SuffixableElement interface class.
I have the strong feeling I am missing some basics here. I think there is a basic design or logic error. Also i have to admit, that I am not a C++ pro, means I am still a C++ beginner, So please bear with me....
Let me show you the code I have:
The class definition:
using namespace std;
// --- class represents a stemmed term of a StemmedSentence object
class StemmedTerm {
private:
string _word;
string _stemmed;
public:
StemmedTerm(string word, string stemmed);
~StemmedTerm();
// --- Implementation of StemmedTerm interface
string getTerm();
string getStemmed();
};
class StemmedSentence;
class SuffixableElement;
// --- a snippet containing stemmed sentences
class ArrayStemmedSnippet {
friend class StemmedSentence;
public:
ArrayStemmedSnippet();
ArrayStemmedSnippet(Array snippetTerms);
~ArrayStemmedSnippet();
SuffixableElement * getSentence(int n);
int size() const;
private:
Array snippet;
Array sentences;
};
// --- a stemmed sentence from ArrayStemmedSnippet
class StemmedSentence : public ArrayStemmedSnippet {
public:
StemmedSentence(ArrayStemmedSnippet *p);
StemmedSentence(const int start, const int end);
virtual ~StemmedSentence();
virtual void * get(int index) const;
virtual int size() const;
private:
int _start;
int _end;
ArrayStemmedSnippet *parent;
};
// --- interface to access a stemmed sentence and its size
class SuffixableElement {
public:
virtual ~SuffixableElement() = 0;
virtual void * get(int index) const = 0;
virtual int size() const = 0;
};
The implementation:
using namespace std;
StemmedSentence::StemmedSentence(const int start, const int end)
: _start(start), _end(end) {
}
StemmedSentence::StemmedSentence(ArrayStemmedSnippet *p)
: parent( p ) {
}
StemmedSentence::~StemmedSentence() {
}
// --- implementation interface SuffixableElement
void * StemmedSentence::get(int index) const {
if (index == (size() - 1)) {
return NULL; // --- End of string (sentence)
}
// get the stemmed term from snippets array in ArrayStemmedSnippet class
// is null, because array is not accessable thru the Suffixable iFace / eg not defined
return snippet[ _start + index ];
}
// --- implementation interface SuffixableElement
int StemmedSentence::size() const {
return _end - _start + 2;
}
// --- add array of snippet terms to sentences. NULL represents end of sentence
ArrayStemmedSnippet::ArrayStemmedSnippet() { }
ArrayStemmedSnippet::ArrayStemmedSnippet(Array snippetTerms)
: snippet( snippetTerms ) {
int index = 0;
for (int i = 0; i < snippetTerms.getLength(); i++) {
if (snippetTerms[ i ] == NULL) {
sentences.append( new StemmedSentence(index, i - 1 ));
index = i + 1;
}
}
}
ArrayStemmedSnippet::~ArrayStemmedSnippet() {
for (int i = 0; i < sentences.getLength(); i++) {
delete sentences[i];
}
}
int ArrayStemmedSnippet::size() const {
return sentences.getLength();
}
// --- returns n-th sentence of this snippet
SuffixableElement * ArrayStemmedSnippet::getSentence(int n) {
StemmedSentence( this ); // --- just a try: try passing base instance to derived class
return (SuffixableElement*)sentences[ n ];
}
The main body:
int main() {
// Sentence 1:
Array stemmed;
StemmedTerm *st1 = new StemmedTerm( "Mouse", "Mouse" );
StemmedTerm *st2 = new StemmedTerm( "ate", "ate" );
StemmedTerm *st3 = new StemmedTerm( "cheese", "cheese" );
stemmed.append( st1 );
stemmed.append( st2 );
stemmed.append( st3 );
stemmed.append( NULL ); // ---- end of snippet sentence
// Sentence 2:
StemmedTerm *st21 = new StemmedTerm( "Cat", "Cat" );
StemmedTerm *st22 = new StemmedTerm( "ate", "ate" );
StemmedTerm *st23 = new StemmedTerm( "cheese", "cheese" );
StemmedTerm *st24 = new StemmedTerm( "too", "too" );
stemmed.append( st21 );
stemmed.append( st22 );
stemmed.append( st23 );
stemmed.append( st24 );
stemmed.append( NULL ); // ---- end of snippet sentence
// ok -- one stemmedsnippet with 2 sentences
ArrayStemmedSnippet ass( stemmed );
// do some sother stuff ... later in the program ....
// --- get elements and size info thru SuffixableElements interface
SuffixableElement *currentElement = (SuffixableElement *)ass.getSentence(1);
cout << "cur. element size=" << currentElement->size() << endl;
StemmedTerm *st = (StemmedTerm*)currentElement->get(2, ass);
string str = st->toString();
cout < "second word is=" << str << endl;
delete st1;
delete st2;
delete st3;
delete st21;
delete st22;
delete st23;
delete st24;
return 1;
}
I skipped some function from above (toString() and other not relevant fct) to keep thecode as short as possible.
So the main problem is when I call
SuffixableElement * currentElement = (SuffixableElement *)ass.getSentence(1);
// --- this works fine! cout << "cur. element size=" << currentElement->size() << endl;
// --- does not work, because snippet array of base class is uninitalized coming thru the // --- interface .... // --- Also passing the instance of the base class ArrayStemmedSnippet with the // --- getSenctence() Fct does not help. StemmedTerm * st = (StemmedTerm *)currentElement->get(2); // should return "cheese" string str = st->toString(); cout << "str=" << str << endl;
What do I have to to that the snippet array is accessable thru the get(int index) callwith the SuffixableElement interface. The size() Fct is no problem, since I instanciated the derived class and access local members _start/_end.Also StemmedSentence class as a nested class within ArrayStemmedSnippet is not working, unlike Java (I think) C++ wont let me access objects of ArrayStemmedSnippet like that.So I am sure I have some basic misunderstanding.
What I am missing???
Any help is greatly appreciated!!
// --- returns n-th sentence of this snippet
SuffixableElement* ArrayStemmedSnippet::getSentence(int n) {
// ...
return (SuffixableElement*)sentences[ n ];
}
'sentences' is an array of objects of the StemmedSentence class. StemmedSentence is not derived from SuffixableElement, that is it does not provide that interface.
Try the following:
class StemmedSentence : public ArrayStemmedSnippet, public SuffixableElement {
// ...
};
and
SuffixableElement* ArrayStemmedSnippet::getSentence(int n) {
StemmedSentence* sentence = sentences[ n ];
sentence->setParent(this);
return sentence;
}
这篇关于从派生类访问基类成员变量,从派生类实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!