从派生类访问基类成员变量,从派生类实例化

时间:2010-03-05 07:53:59

标签: c++

通过包含2个虚函数的接口从派生类访问基类成员变量时遇到问题。主要目的是我需要在2个不同的程序阶段使用这些类。 第一阶段是通过ArrayStemmedSnippet类用文本片段/句子填充数组 第二阶段,在程序的稍后阶段,通过SuffixableElement接口类从基类中定义的句子数组/片段数组中获取项目。

我有强烈的感觉,我在这里缺少一些基础知识。我认为存在基本设计或逻辑错误。另外我不得不承认,我不是C ++专业人士,意味着我仍然是C ++初学者,所以请耐心等待....

让我告诉你我的代码:

班级定义:


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;
};

实施:


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 ]; }

主体:


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;
} 

我从上面跳过了一些函数(toString()和其他不相关的fct)来保持 代码尽可能短。

所以主要问题是我打电话

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;

我需要通过get(int index)调用来访问代码段数组 使用SuffixableElement接口。 size()Fct没问题,因为我实例化了派生类并访问了本地成员_start / _end。 另外,StemmedSentence类作为ArrayStemmedSnippet中的嵌套类是行不通的,不像Java(我认为)C ++不会让我像那样访问ArrayStemmedSnippet的对象。所以我确信我有一些基本的误解。

我缺少什么?

非常感谢任何帮助!!

2 个答案:

答案 0 :(得分:1)

// --- returns n-th sentence of this snippet
SuffixableElement* ArrayStemmedSnippet::getSentence(int n) {
    // ...
    return (SuffixableElement*)sentences[ n ];
}

'sentences'是StemmedSentence类的一个对象数组。 StemmedSentence不是从SuffixableElement派生的,也就是说它不提供该接口。

尝试以下方法:

class StemmedSentence : public ArrayStemmedSnippet, public SuffixableElement {
    // ...
};

SuffixableElement* ArrayStemmedSnippet::getSentence(int n) {
    StemmedSentence* sentence = sentences[ n ];
    sentence->setParent(this);
    return sentence;
}

答案 1 :(得分:0)

如果SuffixableElement是您的界面,则StemmedSentence必须从中获取。