Sunday, July 1, 2007

know how to access names in templatized base classes

when compilers encounter the definition of derived class they dont know what class it inherits from, so they wont find functions defined in the base class, since it doesnt know which base template it is inheriting from ( c++ does this base may actually be translates to a total template specialization that doesnt define the particular function in question)

so basically in templates inheritance stops working.
three ways around this :

prefix calls to base class functions with this-> ( why does it work ?...as in how does it work underneath?)
use a using declaration e,g,
template
class logginmsgsender:public msgsender {
public:
using msgsender::sendclear ;
void sendclearmsg(msginfo& info)
{
sendClear(info);
}
};

or you can exactly specify the function being called in the base class as in
msgsendder::sendclear(info)

its least desirable because if senclear is virtual than the effect of virtual is lost

note that all of these solutions basically promise the compiler that the definition of the function will be available, and so when it sees a call to senclearmsg and if it found that the expected function is not available, then it will still give you a compiler error.

so basically the availablity of a function call or something else is checked only when its usage is encountered.....so if i declare a class that doesnt define sendclear function but neither uses sendclearmsg function than it will compile and execute without error ? is that right ?

things to remember:
in derived class templates refer to names in base class templates via a this->prefix via using declarations or via an explicit base class qualification.

No comments: