By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

NULL template?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.