User specified inheritance
-
I am reworking a template library that is VC++6.0 compatible, but I do not have access to that compiler at the moment. Will the following test successfully compile and run on VC++6.0?
// JRS - 7.25.2010 - Test user specified inheritance #include <cstdlib> #include <iostream> using namespace std; template<typename CharT> struct A { CharT ch; A() : ch('A') {} }; template<typename CharT, typename BaseT = A<CharT> > struct B : public BaseT // user specified inheritance { B() { BaseT::ch = 'B'; } }; int main(int argc, char* argv[]) { A<char> a; B<char> b; cout << "a.ch = " << a.ch << endl; cout << "b.ch = " << b.ch << endl; system("PAUSE"); return 0; }
Curious? I have 3 class templates that all have a “has-a” relationship with a data storage class template and I am changing it to an “is-a” relationship. But I still want the user to be able specify the storage class, as there are legitimate reasons why they may want to. The above method solves the problem, but I have never seen it used by anyone else.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
-
I am reworking a template library that is VC++6.0 compatible, but I do not have access to that compiler at the moment. Will the following test successfully compile and run on VC++6.0?
// JRS - 7.25.2010 - Test user specified inheritance #include <cstdlib> #include <iostream> using namespace std; template<typename CharT> struct A { CharT ch; A() : ch('A') {} }; template<typename CharT, typename BaseT = A<CharT> > struct B : public BaseT // user specified inheritance { B() { BaseT::ch = 'B'; } }; int main(int argc, char* argv[]) { A<char> a; B<char> b; cout << "a.ch = " << a.ch << endl; cout << "b.ch = " << b.ch << endl; system("PAUSE"); return 0; }
Curious? I have 3 class templates that all have a “has-a” relationship with a data storage class template and I am changing it to an “is-a” relationship. But I still want the user to be able specify the storage class, as there are legitimate reasons why they may want to. The above method solves the problem, but I have never seen it used by anyone else.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
John R. Shaw wrote:
Will the following test successfully compile...on VC++6.0?
Yes.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
John R. Shaw wrote:
Will the following test successfully compile...on VC++6.0?
Yes.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
Thanks! :-D
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone