[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

debug message in a template class

thomas

9/30/2008 6:46:00 AM

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?
4 Answers

Barry

9/30/2008 9:01:00 AM

0

On Sep 30, 2:46 pm, thomas <FreshTho...@gmail.com> wrote:
> 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.

thomas

9/30/2008 9:19:00 AM

0


>
> 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.

James Kanze

9/30/2008 10:01:00 AM

0

On Sep 30, 11:18 am, thomas <FreshTho...@gmail.com> wrote:

[...]
> > Or you may have typeid(T).name() output as message.- Hide
> > quoted text -

> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Barry

9/30/2008 11:38:00 AM

0

On Sep 30, 5:18 pm, thomas <FreshTho...@gmail.com> wrote:
> > 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?
>

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();
}


>
>
> > Or you may have typeid(T).name() output as message.- Hide quoted text -
>
> > - Show quoted text -
>
> Yeah.. The typeid function should work. Thanks.