473,394 Members | 1,752 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,394 software developers and data experts.

C'tors and d'tors in templates?

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
Jul 19 '05 #1
2 1844
"Espen Ruud Schultz" <li*****@online.no> wrote in message
news:_K*******************@news4.e.nsc.no

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
Jul 19 '05 #2
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.

Jul 19 '05 #3

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

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
18
by: Zytan | last post by:
I just got a TypeInitializationException exception. Why it is a common cause for this? The InnerException is System.FormatException, although I don't see anything requesting something to be...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.