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

NULL template?

I have a template with some common functions:

template< class Type > class Object : public Type {

public:

void Function( void );

};

Sometimes it makes sence to run Function() without specifying a Type. Is it
possible to create an object out of Object that doesn't inherit like this
template does. Darn, not easy to explain. I wanna do something like this:

Object< NULL >::Function();

....and this:

Object< NULL > This;
This.Function();

TIA!

, Espen
Jul 19 '05 #1
3 5105
Espen Ruud Schultz wrote:
I have a template with some common functions:

template< class Type > class Object : public Type {

public:

void Function( void );

};

Sometimes it makes sence to run Function() without specifying a Type.
This is where I loose you.

The above template says that function is dependant on type and then you
say it's not.

Anyhow, while I don't really know how to help you you might find some
ideas in partial template specializations.

Is it possible to create an object out of Object that doesn't inherit like this
template does. Darn, not easy to explain. I wanna do something like this:

Object< NULL >::Function();

...and this:

Object< NULL > This;
This.Function();


This might be what you're looking for.

BTW - NULL explands to 0 - you can't use Object< NULL >.

#include <iostream>

template <typename Type> class Object : public Type
{
public:

void Function()
{
std::cout << "Basic template\n";
}
};

class SomeClass
{
};

class NULLCLASS
{
};
template <> class Object<NULLCLASS>
{
public:

void Function()
{
std::cout << "NULLCLASS\n";
}
};

int main()
{

Object< SomeClass > foo_some;

Object< NULLCLASS > foo_null;
foo_some.Function();
foo_null.Function();

return 0;
}

Jul 19 '05 #2
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bh********@dispatch.concentric.net

This is where I loose you.

The above template says that function is dependant on type and then you
say it's not.


Maybe this example will help you understand:

class TypeBMP {

public:

Load( filename );
Save( filename );

};

template< class Type > class Object : public Type {

public:

DoFileExist( filename );

};

int main( void ) {

// Normal operation:

Object< TypeBMP > ObjectBMP;
...
ObjectBMP.DoFileExist( filename );
ObjectBMP.Save( filename );

// But sometimes I wanna check if a file exists
// without creating a big object:

Object< NULL >::DoFileExist( filename );

// Or use a general object:

Object< NULL > NullObject;
NullObject.DoFileExist( filename );

return 0;

}

I guess the only and closest way is to create an empty NULLCLASS as you
suggested...

, Espen
Jul 19 '05 #3
Espen Ruud Schultz wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bh********@dispatch.concentric.net
....
I guess the only and closest way is to create an empty NULLCLASS as you
suggested...


I would not design it this way.

I would have a file object that knows about files and a file type object
that knows how to read files.

This is a classic trying to do too many things in one class problem.

G

Jul 19 '05 #4

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

Similar topics

19
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the...
37
by: red floyd | last post by:
I searched the FAQ on this one, couldn't really find an answer. Stylistically, is it better to use 0 or NULL? I know that pre-Standard, 0 was recommended, because some compilers implemented...
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
9
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
7
by: neelsmail | last post by:
Hi, I want to give default value as NULL/0 for non-type template parameter. I using SunStudio on Linux. I have tried following: #define non_closer ((int(*)(FILE*))0L) template<class T, int...
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
1
by: bob | last post by:
Hi, How can you bind a Calendar control in a template field when the data is null? i.e. The bound data is a null date. I would like to use a calendar control in the edit template to update the...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
0
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...
0
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...
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.