Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem in calling pure virtual function

Reply
Thread Tools

problem in calling pure virtual function

 
 
engineer
Guest
Posts: n/a
 
      03-06-2013
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
 
Reply With Quote
 
 
 
 
engineer
Guest
Posts: n/a
 
      03-06-2013
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


 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      03-06-2013
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-06-2013
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
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      03-06-2013
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need clarification for virtual method and pure virtual function a C++ 7 06-28-2007 12:38 PM
virtual vs pure virtual member function sam_cit@yahoo.co.in C++ 7 05-02-2007 10:00 AM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM



Advertisments