problem in calling pure virtual function

E

engineer

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
 
E

engineer

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
 
Ö

Öö Tiib

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.
 
J

James Kanze

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.)
 
Ö

Öö Tiib

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top