简体   繁体   中英

c++ inheritance design issue

In c++ I want to have an array of the abstract type Query which has the function calcScore() which is a pure virtual function.
And I have two classes which are non-abstract: ConQuery and DisQuery that implements the calcScore function.

In order to do I defined the array like this:

vector<Query*> m;

and I iterate and call the function like this:

for (vector<Query*>::const_iterator it1 = index.begin() ;it1 != index.end() ; it1++)
{
     cout << (*it1)->CalcScore() << endl; 
}

I get an error for calling a pure virtual function of Query. How do I make it call to the function of ConQuery or the one of DisQuery by the polymorphic type? thanks.

The error can only occur in the language if you try to call a pure virtual function from the constructor or destructor of the type (where the most derived type is not yet built/has already been destroyed):

struct Query {
   virtual void f() = 0;
   Query() {
      f();                // !!
   }
   ~Query() {
      f();                // !!
   }
};

Note that compilers usually flag the code above as an error, but will fail to detect it if the call is not directly in the constructor/destructor, if for example you pass a reference to the object to a different function that performs the call.

Are you sure CalcScore is implemented in ConQuery and DisQuery ? I tried this:

#include <iostream>
#include <vector>

class Query{
public:
    virtual int CalcScore() = 0;
};

class Query2 : public Query
{
public:
    virtual int CalcScore()
    {

        return 2;
    }
};

class Query3 : public Query
{
public:
    virtual int CalcScore()
    {

        return 3;
    }
};

int main(int argc, char* argv[])
{

    std::vector<Query*> m;
    m.push_back(new Query2());
        m.push_back(new Query3());
    for (std::vector<Query*>::const_iterator it1 =  m.begin() ;it1 !=  m.end() ; it1++)
    {
        std::cout  << (*it1)->CalcScore();
    }
}

and it works fine under my VS2012.

Also I used it tons of times in some of my projects.

Maybe you try to push_back a Query item ( and not a con/dis ) ?

Change

for (vector<Query>::const_iterator it1 = index.begin() ;it1 != index.end() ; it1++)

to

for (vector<Query *>::const_iterator it1 = index.begin() ;it1 != index.end() ; it1++)

I might have missed something but this works for me: I already know that i is not initialised etc ;-)

#include "stdafx.h"
#include <vector>


class Query
{
public:
    int i;
    void virtual CalcScore() = 0;
};

class ConQuery :public Query
{
public:
    int i;
    void virtual CalcScore() {i++;}

};


int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<Query*> index;
    ConQuery b;

    index.push_back(&b);

    for (std::vector<Query*>::const_iterator it1 = index.begin() ;it1 != index.end() ; it1++) 
    {      
        (*it1)->CalcScore();
    } 

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM