NULL template?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Espen Ruud Schultz

    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


  • Gianni Mariani

    #2
    Re: NULL template?

    Espen Ruud Schultz wrote:[color=blue]
    > 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.[/color]

    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[color=blue]
    > 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() ;
    >[/color]

    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<NULLCLAS S>
    {
    public:

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



    int main()
    {

    Object< SomeClass > foo_some;

    Object< NULLCLASS > foo_null;


    foo_some.Functi on();
    foo_null.Functi on();

    return 0;
    }

    Comment

    • Espen Ruud Schultz

      #3
      Re: NULL template?

      "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
      news:bhm1t1$m4n @dispatch.conce ntric.net[color=blue]
      >
      > This is where I loose you.
      >
      > The above template says that function is dependant on type and then you
      > say it's not.
      >[/color]

      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.DoFil eExist( filename );
      ObjectBMP.Save( filename );

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

      Object< NULL >::DoFileExis t( filename );

      // Or use a general object:

      Object< NULL > NullObject;
      NullObject.DoFi leExist( filename );

      return 0;

      }

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

      , Espen


      Comment

      • Gianni Mariani

        #4
        Re: NULL template?

        Espen Ruud Schultz wrote:[color=blue]
        > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
        > news:bhm1t1$m4n @dispatch.conce ntric.net
        >[/color]
        ....[color=blue]
        >
        > I guess the only and closest way is to create an empty NULLCLASS as you
        > suggested...
        >[/color]

        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

        Comment

        Working...