NULL template?

E

Espen Ruud Schultz

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
 
G

Gianni Mariani

Espen said:
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;
}
 
E

Espen Ruud Schultz

Gianni Mariani said:
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
 
G

Gianni Mariani

Espen said:
....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top