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

  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,708
    >> 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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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,708
    the closest thing we have to that is lambda functions.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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, 12:54 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM