473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template Type Functions

I have a class temple which has to handle strings differently. Only a few
functions have to handle the string type differently. Is there anyway I can
do this without having two 'complete' classes, one generic and one for
strings only. I would just like to define the functions which are
different.

// Class Header example (there are other funcitons - this is just
// to show what I have done - and what I am trying to do.

tempate <class T>
class xList
{
public:
xList();
~xList();
T GetListVal();
void SetListVal( T );
private:
T *Ptr;
T Val;
};

tempate <>
class xList<std::stri ng>
{
public:
xList();
~xList();
string GetListVal();
void SetListVal( string );
private:
string Val;
};

The only function that will be different is the 'string GetListVal()' - is
there anyway I can just define a special 'string' function for the
GetListVal() when the class is typed as string?

Thanks
Mike
Jul 23 '05 #1
2 1010
Mike wrote:
I have a class temple which has to handle strings differently. Only a few
functions have to handle the string type differently. Is there anyway I can
do this without having two 'complete' classes, one generic and one for
strings only. I would just like to define the functions which are
different.

// Class Header example (there are other funcitons - this is just
// to show what I have done - and what I am trying to do.

tempate <class T>
class xList
{
public:
xList();
~xList();
T GetListVal();
void SetListVal( T );
private:
T *Ptr;
T Val;
};

tempate <>
class xList<std::stri ng>
{
public:
xList();
~xList();
string GetListVal();
void SetListVal( string );
private:
string Val;
};

The only function that will be different is the 'string GetListVal()' - is
there anyway I can just define a special 'string' function for the
GetListVal() when the class is typed as string?


You may provide an explicit specialisation of that function for 'string'
type instead of the whole class:

template<> string xList<string>:: GetListVal() {
blah blah blah;
return somestring;
}

The rest of the class implementation will be picked by instantiating the
template members.

V
Jul 23 '05 #2
Mike wrote:
I have a class temple which has to handle strings differently. Only a
few functions have to handle the string type differently. Is there
anyway I can do this without having two 'complete' classes, one generic
and one for strings only. I would just like to define the functions
which are different.

// Class Header example (there are other funcitons - this is just
// to show what I have done - and what I am trying to do.

tempate <class T>
class xList
{
public:
xList();
~xList();
T GetListVal();
void SetListVal( T );
private:
T *Ptr;
T Val;
};

tempate <>
class xList<std::stri ng>
{
public:
xList();
~xList();
string GetListVal();
void SetListVal( string );
private:
string Val;
};

The only function that will be different is the 'string GetListVal()' -
is there anyway I can just define a special 'string' function for the
GetListVal() when the class is typed as string?

"Victor Bazarov" wrote:
You may provide an explicit specialisation of that function for 'string'
type instead of the whole class:

template<> string xList<string>:: GetListVal() {
blah blah blah;
return somestring;
}

The rest of the class implementation will be picked by instantiating the
template members.

V


Thanks for response, that is exactly what I was looking for.

Mike
Jul 23 '05 #3

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

Similar topics

6
2153
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a binary search tree. Note there is a requirement for this to be a Win32/MFC "friendly" class, thus the use of CObject and POSITION. There is also a requirement for there not to be a separate iterator class. template <class TYPE, class ARG_TYPE =...
3
5512
by: Max | last post by:
I am trying to find a way to eliminate vararg functions from my code by packaging the input parameters in stringstreams. Here is an oversimplified example of what I am trying to do: Functions FUNC1 and FUNC2 delegate to functions func1 and func2, respectively. The goal is to delegate to func1 and func2 using an identical interface provided by the template function template<typename F>FTEMPL that takes a stringstream as input and...
8
11359
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
9
2305
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
9
2725
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? template <class T1, class T2> int getValue( T1 col, T2 row ) ; template <class T1, class T2> double getValue( T1 col, T2 row ) ;
6
3996
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
3
3933
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. template<class Type> class base { public: base& operator/=(const base&); Type *image;
3
3749
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
5
1739
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; // function object
5
2262
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.