debug message in a template class

T

thomas

template <class T>
class BufferQueue{
public:
int BufferQueue<T>::pushToQueue(T pkt, int timeout);
}

template<class T>
int BufferQueue<T>::pushToQueue(T pkt, ACE_Time_Value *timeout){
cout<<"in pushToQueue"<<endl; --------Line p
return 0;
}

Suppose I have two instances BufferQueue<A> _a, BufferQueue<B> _b;
I want to know which instance I'm in by printing some debug messages
out in Line p
like
"in pushToQueue of A", etc.

How can I tell the compiler or change Line p to make it?
 
B

Barry

template <class T>
class BufferQueue{
public:
          int BufferQueue<T>::pushToQueue(T pkt, int timeout);

}

template<class T>
int BufferQueue<T>::pushToQueue(T pkt, ACE_Time_Value *timeout){
          cout<<"in pushToQueue"<<endl;               --------Line p
          return 0;

}

Suppose I have two instances BufferQueue<A> _a, BufferQueue<B> _b;
I want to know which instance I'm in by printing some debug messages
out in Line p
like
"in pushToQueue of A", etc.

How can I tell the compiler or change Line p to make it?

You can define a static memeber function like T::GetName() which
returns
the name of the class, so

cout << "in pushToQueue " << T::GetName() << endl;

Or you may have typeid(T).name() output as message.
 
T

thomas

You can define a static memeber function like T::GetName() which
returns
the name of the class, so

cout << "in pushToQueue " << T::GetName() << endl;

How can you write the "GetName" method?
Or you may have typeid(T).name() output as message.- Hide quoted text -

- Show quoted text -

Yeah.. The typeid function should work. Thanks.
 
J

James Kanze

[...]
Yeah.. The typeid function should work. Thanks.

It should, but whether it does or not is a quality of
implementation issue. As far as the standard is concerned, an
implementation which always returns "" is fully conforming. In
practice, most implementations (with the notable exception of
g++) do return something useful.
 
B

Barry

How can you write the "GetName" method?

template <class T>
struct MyClass
{
void MyFunc() const
{
std::cout << T::GetName() << std::endl;
}
};

Here every instatiation of T is requred
to have a static function named "GetName";

struct T_type1
{
static char const* GetName() { return "T_type1"; }
};

int main()
{
MyClass<T_type1> c1;
c1.MyFunc();
}
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top