problem in calling pure virtual function

Discussion in 'C++' started by engineer, Mar 6, 2013.

  1. engineer

    engineer Guest

    Hi,

    I have three classes NsObject, Biconnector, Phy.
    class NsObject is parent class and has a pure virtual function 'recv( )'.
    class Biconnector derived from NsOject.
    class Phy derived from Biconnector.
    both Biconnector and Phy have their own implementation for function recv.

    class NsObject {
    public:

    Virtual void recv( ) = 0; //so this is pure virtual function
    };

    //---------------------------------------------------
    class Biconnector : public NsObject {
    Protected:
    Void recv( ) {
    Cout<<” I am biconnector: “;
    }
    NsObject * uptarget;
    }

    //---------------------------------------

    class phy: public biconnector {
    Public:
    Void recv( ) {
    uptarget->recv( ); //which function does this line calls
    }
    }

    //------- End of coding-------------


    If I call phy::recv(), it will in turn execute uptarget->recv ( );,

    This confuses me, as uptarget is a pointer of type NsObject and NsObject does not have implementation for recv ( ).
    so which statements will be executed for "uptarget->recv( )"
    thanks in advance.
    - Ahmed
     
    engineer, Mar 6, 2013
    #1
    1. Advertising

  2. engineer

    engineer Guest

    Andy,
    thanks for ur reply,
    Apart from these 'mixed case' errors the code is also incomplete,..As the original code has more than 100 classes and it runs perfectly,...however I am trying to follow the code but couldn't understand these particular lines.
    Need someone's help about it.
    -bilal

    On Wednesday, March 6, 2013 7:09:16 PM UTC+9, Andy Champ wrote:
    > On 06/03/2013 07:44, engineer wrote:
    >
    > >

    >
    > > This confuses me, as uptarget is a pointer of type NsObject and NsObject does not have implementation for recv ( ).

    >
    > > so which statements will be executed for "uptarget->recv( )"

    >
    > > thanks in advance.

    >
    > > - Ahmed

    >
    > >

    >
    > Ahmed,
    >
    >
    >
    > the code sample you provided won't build - you've got typing errors
    >
    > (mixed case for biconnector for a start).
    >
    >
    >
    > However I think your question will be answered if you read
    >
    >
    >
    > <http://www.parashift.com/c++-faq/virtual-functions.html>
    >
    >
    >
    > If not come back to us.
    >
    >
    >
    > Andy
     
    engineer, Mar 6, 2013
    #2
    1. Advertising

  3. engineer

    Öö Tiib Guest

    On Wednesday, 6 March 2013 14:43:42 UTC+2, engineer wrote:
    >
    > Apart from these 'mixed case' errors the code is also incomplete,..As
    > the original code has more than 100 classes and it runs perfectly,...
    > however I am trying to follow the code but couldn't understand
    > these particular lines.
    >
    > Need someone's help about it.


    Objects of type NsObject can not be made without overriding 'void recv()'
    in it. So pointer to NsObject can be nullptr or point to object of
    type that is derived from NsObject and that overrides the function.

    If it is nullptr then biconnector::recv() is dereferencing it and that
    is undefined by standard but most implementations it is certain crash.

    If it is pointer to derived then biconnector::recv() will call most
    derived override.
     
    Öö Tiib, Mar 6, 2013
    #3
  4. engineer

    James Kanze Guest

    On Wednesday, March 6, 2013 1:04:34 PM UTC, Öö Tiib wrote:
    > On Wednesday, 6 March 2013 14:43:42 UTC+2, engineer wrote:


    > > Apart from these 'mixed case' errors the code is also incomplete,..As
    > > the original code has more than 100 classes and it runs perfectly,...
    > > however I am trying to follow the code but couldn't understand
    > > these particular lines.


    > > Need someone's help about it.


    > Objects of type NsObject can not be made without overriding 'void recv()'
    > in it. So pointer to NsObject can be nullptr or point to object of
    > type that is derived from NsObject and that overrides the function.


    That's not quite true. If uptarget is set using the this
    pointer in the base class constructor of another object, it will
    point to an NsObject. And calling recv through it will resolve
    to the pure virtual function, which in turn is undefined
    behavior. (Most compilers will in fact cause the program to
    abort with an error message, but I seem to recall one in which
    it would call the function in one of the derived classes, even
    though the class hadn't been constructed, and some older
    compilers would abort with no error message.)

    --
    James
     
    James Kanze, Mar 6, 2013
    #4
  5. engineer

    Öö Tiib Guest

    On Wednesday, 6 March 2013 19:14:19 UTC+2, James Kanze wrote:
    > On Wednesday, March 6, 2013 1:04:34 PM UTC, Öö Tiib wrote:
    > > On Wednesday, 6 March 2013 14:43:42 UTC+2, engineer wrote:

    >
    > > > Apart from these 'mixed case' errors the code is also incomplete,..As
    > > > the original code has more than 100 classes and it runs perfectly,...
    > > > however I am trying to follow the code but couldn't understand
    > > > these particular lines.

    >
    > > > Need someone's help about it.

    >
    > > Objects of type NsObject can not be made without overriding 'void recv()'
    > > in it. So pointer to NsObject can be nullptr or point to object of
    > > type that is derived from NsObject and that overrides the function.

    >
    > That's not quite true. If uptarget is set using the this
    > pointer in the base class constructor of another object, it will
    > point to an NsObject. And calling recv through it will resolve
    > to the pure virtual function, which in turn is undefined
    > behavior. (Most compilers will in fact cause the program to
    > abort with an error message, but I seem to recall one in which
    > it would call the function in one of the derived classes, even
    > though the class hadn't been constructed, and some older
    > compilers would abort with no error message.)


    Yes. The reason I omitted mentioning it is that calling virtual functions
    of partially constructed objects (even when it is not pure so it is
    perfectly legal and well-defined what happens unlike here) is confusing to
    most readers of code. At least for me it takes always few "wtf"s to figure
    it out.

    Other cases I omitted were that pointer to NsObject may be uninitialized,
    it may point to object that is partially or fully destroyed and it can point
    to one past last element in some array of derived objects. Such pointers
    will also cause lot of fun and are not guaranteed by standard to crash if dereferenced.

    I was worried that burdening OP with such corner cases may stop him from
    understanding the basics that he asked for.
     
    Öö Tiib, Mar 6, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IK
    Replies:
    2
    Views:
    637
    hemraj
    Jul 23, 2004
  2. Andreas Lagemann
    Replies:
    8
    Views:
    531
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    461
  4. Replies:
    7
    Views:
    612
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    383
    dasjotre
    Jun 28, 2007
Loading...

Share This Page