Possible to define BaseClass functions where derived's have diff. return type/args?

This is a discussion on Possible to define BaseClass functions where derived's have diff. return type/args? within the C++ Programming forums, part of the General Programming Boards category; I want to create a base class that tells people that there will be two methods but not specify what ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Possible to define BaseClass functions where derived's have diff. return type/args?

    I want to create a base class that tells people that there will be two methods but not specify what the args or return type are. How would I do this?

    Code:
    //Something along the lines of
    template <typename T>
    class Test
    {
    public:
             T load(SomeClass a);
             void save(SomeClass a, T b);
    };
    
    class DerivedA : public Test
    {
    public:
             File load(SomeClass a);
             void save(SomeClass a, File b);
    };
    
    class DerivedB : public Test
    {
    public:
             Text load(SomeClass a);
             void save(SomeClass a, Text b);
    };
    Is that valid? is there a better way?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> Is that valid?

    you have to pass the type to the template, eg:

    class DerivedA : public Test<File>

    >> is there a better way?

    possibly. without a more detailed example it's difficult to say, though.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Thanks. One related question:

    In Java if you have a class (or interface) defintion, you can create a class on the fly without having to predefine it. Is this possible in C++?

    Code:
    //If I have interface DataObject
    class Some
    {
    private:
         PrivateClass d;
    
    public:
         DataObject get()
         {
              return new DataObject()
              {
                    public getData()
                    {
                         return d.data();
                    }
              };
         }    
    };

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    In Java if you have a class (or interface) defintion, you can create a class on the fly without having to predefine it.
    What do you mean? Perhaps you have a Java example?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Note that C++ does not support get/set. You have to create your own functions or use a specialized homebrewn class for that.
    Also note that you can't use classes or types which have not yet been defined (you must have the proper definition before you can use the type or class).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by laserlight View Post
    What do you mean? Perhaps you have a Java example?
    Code:
    interface ActionListener
    {
           public void actionPerformed(ActionEvent e);
    }
    
    class Test
    {
           public ActionListener getListener() 
           {
                  //This returns an object that is of class type ActionListener 
                  return new ActionListener() 
                  {
                          public void actionPerformed(ActionEvent e) 
                          {
                                 // do something 
                          }
                  };
            )
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    the closest thing we have to that is lambda functions.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    C++ has local classes, but they're pretty much useless. It doesn't have anonymous local classes like Java.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21