C'tors and d'tors in templates?

E

Espen Ruud Schultz

Have a look at this code:

#include <iostream>
class TypeTXT {
public:
TypeTXT( void ) { std::cout << " TypeTXT" << std::endl; }
~TypeTXT( void ) { std::cout << "~TypeTXT" << std::endl; }
void Function( void ) { std::cout << "TypeTXT::Function" <<
std::endl; }
};
class TypeBMP {
public:
TypeBMP( void ) { std::cout << " TypeBMP" << std::endl; }
~TypeBMP( void ) { std::cout << "~TypeBMP" << std::endl; }
void Function( void ) { std::cout << "TypeBMP::Function" <<
std::endl; }
};
template< class Object > class DataObject {
public:
DataObject( void ) { std::cout << " DataObject" << std::endl; }
~DataObject( void ) { std::cout << "~DataObject" << std::endl; }
void Function( void ) { std::cout << "DataObject::Function" <<
std::endl; }

};
int main( void ) {
DataObject< TypeTXT > ObjectTXT;
DataObject< TypeBMP > ObjectBMP;
ObjectTXT.Function();
ObjectBMP.Function();
return 0;
}

I guess you can see what I'm trying to do. First I want to execute the
c'tor and d'tor of the two classes TypeTXT and TypeBMP. I also want to
execute functions belonging to these classes through the template class.
How do I do all this?

TIA!

, Espen
 
E

Espen Ruud Schultz

Espen Ruud Schultz said:
I guess you can see what I'm trying to do. First I want to execute the
c'tor and d'tor of the two classes TypeTXT and TypeBMP. I also want to
execute functions belonging to these classes through the template class.
How do I do all this?

Nevermind, a shower did the trick. I of course have to create and object
inside the template...

, Espen
 
T

TheWater

This is my opinion:
Why we use template?
Because template can help us grown similar code without copy-and-pasting
frequently.
So, if you want to show TextBMP and TextTXT out, may be you need a string
data member to record the name, and initialize the string on your c'tor.
I recommand you use virtual function, not template.

Regards,
Alvis Jien from Taiwan.
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top