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

Changing a non-template class to a template class easily

gg
Let us say I have the following class -

class X {
int y;
};

Lot of other classes start using X.

Now, I want to make X a template class so that I can write,

template<class T>
class X {
T y;
};

How to minimize the impact on the client?

One way I can think of is, define the template class with a default
argument and with a new name,

template<class T = int>
class X1 {
T y;
};

and then do a typedef,

typedef X1<> X;

So the older clients can continue using the name X and they will get
the desired behavior.

The newer clients can use X1<float> etc.

Is this a good approach and are there any better ones?

Thanx,
gg

Jul 23 '05 #1
2 1268
gg wrote:
Let us say I have the following class -

class X {
int y;
};

Lot of other classes start using X.

Now, I want to make X a template class so that I can write,

template<class T>
class X {
T y;
};

How to minimize the impact on the client?

One way I can think of is, define the template class with a default
argument and with a new name,

template<class T = int>
class X1 {
T y;
};

and then do a typedef,

typedef X1<> X;
There is no need for the default template argument. You could simply
say

typedef X1<int> X;
So the older clients can continue using the name X and they will get
the desired behavior.

The newer clients can use X1<float> etc.

Is this a good approach and are there any better ones?


Looks fine to me.

V
Jul 23 '05 #2


gg schreef:
Let us say I have the following class -

class X {
int y;
};

Lot of other classes start using X.

Now, I want to make X a template class so that I can write,

template<class T>
class X {
T y;
};

How to minimize the impact on the client?


One solution I've used (besides the obvious typedef) is inheritance:
template<class T>
class generic_X { /*** };

class X : public generic_X<int> { /* backwards-compatibility stuff */
};

In some cases, when the difference is too big, private inheritance
can be the better solution.

HTH,
Michiel Salters

Jul 23 '05 #3

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

Similar topics

23
by: Mark Parnell | last post by:
I'm relatively new to PHP, and have just converted a site from ASP to PHP. There is one thing I haven't managed to do, though. When the site was using ASP, I had one file (called variables.asp),...
7
by: kah | last post by:
How do I change a line in a file?? For example I have the follwing text in my file: line1 line2 line3 line4 How do I replace 'line2' with 'newline'. Since the write operation in
9
by: vindhya | last post by:
I have a small query. If I have a character from alphabets and I want to change case of the same then how is it possible ? Here are the solutions, but can you suggest any other solution may be...
5
by: me | last post by:
Hello, I have a question about how too change the background of a command button once it has been click from aliceblue to azure. TIA, Aaron
6
by: reynard | last post by:
I have a page that uses this doctype <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> but when I change to this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
4
by: Jay | last post by:
What's the recommended way to change file/folder names within a project? I want to change the solution name from (say) sol1.sln to sol2.sln, and also everything else named sol1.* (eg sol1.suo,...
2
by: samonline | last post by:
Dear friends, I have written a little program to read the source of a web page into a Rich Text Box. Now I want to find a specific integer value in that text box and take it into a variable. That...
2
by: kolt45 | last post by:
I need to change these three programs to use arrays and i am having a hard time is there anyone that is willing to help? /*homework7a *kolton harper *10-12-07 * *Purpose: */
1
DAF LAD
by: DAF LAD | last post by:
Can anyone help me with this. I have recently taken over as a Membership Secretary of a local sports club. I have a current database that is very old and needs alot of work. Instead of trying to...
2
by: almurph | last post by:
Hi, Hope you can help me wit this one. I have to open either a ".jpg", ".bmp" or a "gif" and alter its dimensions and save the output as a .gif. Here is my attempt - I would greatly...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.