Thread: Inheritance / Return type

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    245

    Inheritance / Return type

    Say I have a class like this:

    Code:
    class Ta {
    private:
    // some class-local vars here
    public:
       void *GetFromStore (int bucket, int id);
    // other functions
    }
    This is the base class for several other classes that need to store data in this type of way. However, each other class uses a different type of pointer, and so has different return types.

    Instead of using casting throughout the program, I overload the function and change the return type:

    Code:
    class Tb : Ta {
    public:
       char *GetFromStore (int bucket, int id);
    }
    The class is then used as follows:

    Code:
       Tb cvar;
       char *p;
    
       p = cvar.GetFromStore (0, 1);
    Where the function GetFromStore would be defined in .cpp file as:

    Code:
    char * Tb::GetFromStore (int bucket, int id)
    {
       return ((char *)Ta::GetFromStore (bucket, id));
    }

    Now, is this the best of dealing with it? I'm suspect not, and think there must be an easier/better way of doing this or calling the previous function (before the overloaded function took over it's name).

    Looking through a few of my C++ books simply says that overloading and just changing the return type is invalid and can't be done, but the above compiles ok with no errors or warnings. If I place code in the Ta class to return a value, then the main code received that value, so the compiler is obviously interpreting it correctly, but it just seems so ugly that there must be a better way of doing this.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Now, is this the best of dealing with it?
    Far from it. If you find yourself using casts, then you're design is broken or you neglected to use a feature of C++ that lets you avoid casting. In this case you have a base class function that needs to be overloaded, but the type of the return value will change with the derived classes. This strikes me as a good time to employ class templates:
    Code:
    #include <iostream>
    
    template <class T>
    class Ta {
    public:
        virtual T* getData() = 0;
    };
    
    template <class T>
    class Tb: public Ta<T> {
        T data;
    public:
        Tb(T init = T()): data(init) {}
        T* getData()
        {
            return &data; // Just to test
        }
    };
    
    int main()
    {
        Tb<int> myT(10);
    
        std::cout<< *myT.getData() <<std::endl;
        
        std::cin.get();
    }
    My best code is written with the delete key.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Templates are a much more type-safe way (void* should usually be avoided where possible). Here is an example:

    Code:
    template<typename T>
    class Ta
    {
    public:
       virtual T* GetFromStore (int bucket, int id);
    };
    
    class Tb : Ta<char>
    {
    public:
       char* GetFromStore (int bucket, int id);
    };
    (Also note I made the function virtual).

    The main drawback is that Ta<char> and Ta<int> (for example) are now different types.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Thanks, I think that'll do the job nicely.

    However, how come Zach uses "template<typename T>" and Prelude uses "template <class T>", but they both seem to do exactly the same thing?

    Also, I assume the "= 0" on the function definition means the function must be overloaded in a derived class or the compilation will fail?

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> However, how come Zach uses "template<typename T>" and Prelude uses "template <class T>", but they both seem to do exactly the same thing?

    They do. I like typename, though most people use class.

    >> Also, I assume the "= 0" on the function definition means the function must be overloaded in a derived class or the compilation will fail?

    Yep
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but they both seem to do exactly the same thing?
    They do. I prefer to use class where the parameter may not be a built-in type, and typename where it must be. Of course, this is all a matter of style.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM