Re: Template classes and containers
From: Alf P. Steinbach (alfps_at_start.no)
Date: 09/02/04
- Next message: Manjesh Gowda: "Why "HTTP CONNECT" disconnects atfer 15 Mins in few proxy server ???"
- Previous message: Prakash Bande: "Template classes and containers"
- In reply to: Prakash Bande: "Template classes and containers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 02 Sep 2004 05:15:48 GMT
* Prakash Bande:
>
> I have a template class as below:
> template <class _T> class myclass{
Underscore followed by uppercase letter is reserved for the
implementation.
> public:
> myclass(){ obj = new _T();}
> _T* obj;
This data member should not be public.
> _T* operator->() (return obj;}
> };
>
> I need to keep all the myclass objects in a STL container such as
> vector. But since every instantiation is a different type I cannot
> have a vector or myclass.
> So I derived myclass from a base class as below:
> class base{
> public:
> base(){}
> ~base(){}
> };
> template class<_T> myclass : public base
STL or standard library container requires among other things that your
objects can be safely copied -- can they?
> Now I have a vector of base objects.
> The problem is that I want to call -> operator. I cannot make it pure
> virtual in base since the return type depends on template resolution.
> I cannot typecast base pointer since I do not know type because of
> template. For example:
> f()
> {
> base *a = new myclass<someclass>
> f1(a);
> }
> void f1(base *a)
> {
> // I want to do this here.
> a->callmethod(); // Would have worked if -> could return
> myclass<someclass>*
> }
Try to explain why you "need" to have these objects in a single container,
or why you "need" to have them of different types.
-- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
- Next message: Manjesh Gowda: "Why "HTTP CONNECT" disconnects atfer 15 Mins in few proxy server ???"
- Previous message: Prakash Bande: "Template classes and containers"
- In reply to: Prakash Bande: "Template classes and containers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|