Click here to Skip to main content
15,883,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sample code:
template <class t=""> class MyTempl1
{
public:
    void F1()
    {
      T* pT = static_cast<t*>(this);

      LPCSTR typname= pT->  xxxx;
      ATLTRACE( "%hs hello world", typname);
    }
};

class Cl1 : 
   public CComObjectRootEx<ccomsinglethreadmodel>,
   public MyTempl1<cl1>
{
};


i use ATL. and for debug log i want to write the name of the outer class "Cl1" in
the template. what can i use for xxx to get it?
Thank you Frank
Posted
Updated 12-Feb-13 4:40am
v2

Hey.

I generally see two ways of doing what you wish (there might be more of course):

1. Use Run-Time Type Information[^], see the link for more details. You could try using the type_info[^] class' name() method.

2. Have a -potentially virtual- function in your classes that can return a string-representation of the class and implement this in every class you use with that template. E.g:
class CMyClass
{
...
virtual LPCTSTR GetClassDebugName() const { return "CMyClass"; }
...
};

...
LPCSTR typname= pT->GetClassDebugName();
...
 
Share this answer
 
Add a typename function to Cl1 and call T::typename() from inside MyTempl1<cl1>::F1()</cl1>.
There's nothing to stop you doing that and it will ensure that any class you use as the template parameter to MyTempl1 will have to have the typename function or the code won't compile.
This is pretty much what's known as 'concept' programming or the first part of it anyway, Bjarne Stroustrup's current favourite idea so you're in good company.
 
Share this answer
 
XML
template <class T> class MyTempl1
{
public:
    void F1()
    {
        const char* typname = typeid(T).name() + 6;
        printf("%s\n", typname);
    }
};
class Cl1 : public MyTempl1<Cl1>
{

};


:D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900