Virtual class problem

Posted by ugur on Stack Overflow See other posts from Stack Overflow or by ugur
Published on 2010-05-02T16:52:57Z Indexed on 2010/05/02 17:47 UTC
Read the original article Hit count: 352

Filed under:
|
|
|

What i think about virtual class is, if a derived class has a public base, let's say, class base, then a pointer to derived can be assigned to a variable of type pointer to base without use of any explicit type conversion. But what if, we are inside of base class then how can we call derived class's functions. I will give an example:

class Graph{
  public:
    Graph(string);
    virtual bool addEdge(string,string);
}
class Direct:public Graph{
  public:  
    Direct(string);
    bool addEdge(string,string);
}
Direct::Direct(string filename):Graph(filename){};

When i call constructor of Direct class then it calls Graph. Now lets think Graph function calls addedge.

Graph(string str){
    addedge(str,str);
}

When it calls addedge, even if the function is virtual, it calls Graph::edge. What i want is, to call Direct::addedge. How can it be done?

© Stack Overflow or respective owner

VIrtual class problem

Posted by ugur on Stack Overflow See other posts from Stack Overflow or by ugur
Published on 2010-05-02T16:52:57Z Indexed on 2010/05/02 16:58 UTC
Read the original article Hit count: 352

Filed under:
|
|
|

What i think about virtual class is, if a derived class has a public base, let's say, class base, then a pointer to derived can be assigned to a variable of type pointer to base without use of any explicit type conversion. But what if, we are inside of base class then how can we call derived class's functions. I will give an example:

class Graph{
  public:
    Graph(string);
    virtual bool addEdge(string,string);
}
class Direct:public Graph{
  public:  
    Direct(string);
    bool addEdge(string,string);
}
Direct::Direct(string filename):Graph(filename){};

When i call constructor of Direct class then it calls Graph. Now lets think Graph function calls addedge.

Graph(string str){
    addedge(str,str);
}

When it calls addedge, even if the function is virtual, it calls Graph::edge. What i want is, to call Direct::addedge. How can it be done?

© Stack Overflow or respective owner

Related posts about virtual

Related posts about c++