473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Template classes and containers

Hi,

I have a template class as below:
template <class _T> class myclass{
public:
myclass(){ obj = new _T();}
_T* obj;
_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

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>*
}

Any suggestions?

Thanks,
Prakash
Jul 22 '05 #1
1 1079
* 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?
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Istvan Buki | last post by:
Dear C++ gurus, I'm having some problems inserting some template classes into containers. I'm looking for ideas, suggestions,... on how to achieve this but first let me explain my problem....
5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
8
by: Simon Elliott | last post by:
Given a template function: template <class T> void foo(T& value) { // do something with value } Is there any way I can tell whether the type T is an STL container?
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
4
by: Mark P | last post by:
I'm trying to define a templatized version of operator<< to print out information about a template class. The code below won't compile. (error: no match for std::ostream& << Outer<int>::Inner&) ...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.