Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ATLtemplate
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 12-Feb-13 4:20am
Edited 12-Feb-13 4:40am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

template <class T> class MyTempl1
{
public:
    void F1()
    {
        const char* typname = typeid(T).name() + 6;
        printf("%s\n", typname);
    }
};
class Cl1 : public MyTempl1<Cl1>
{
 
};
 
Big Grin | :-D
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Add a typename function to Cl1 and call T::typename() from inside MyTempl1::F1().
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 450
1 Sergey Alexandrovich Kryukov 370
2 Shai Vashdi 318
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 9,109
1 OriginalGriff 5,290
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid