Thread: Abstract Interfacing (overiding an initialization function)

  1. #1
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    Abstract Interfacing (overiding an initialization function)

    I'm trying to figure out how to overide a virtual function with a different signature. A function initialize may need to be initialized a different a different way per object (I know this isn't a perfectly abstract interface) but I dont want to define a ton of virtual Init functions in the abstract class and redefine them empty in each inheriting class. Any way I can achieve this?

    Code:
    class AbstractInterface{
    public:
         AbstractInterface();
         ~AbstractInterface();
    
    public:
         virtual void Init();
    };
    
    class AIobject : public AbstractInterface {
    public:
         void Init(bool small);
    };
    
    class AIobject2 : public AbstractInterface {
    public:
         void Init(int size);
    };
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The closest you can get is with return values. If the base class returns a reference to base then you can declare the function virtual and return a derived reference in the derived class. Otherwise you're not overriding Init and polymorphism won't take effect. Perhaps templates would be more to your liking:
    Code:
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    class Interface {
    public:
      virtual void Init(T obj) { cout<<"Interface Init: "<< obj <<endl; }
    };
    
    template <typename T>
    class Derived: public Interface<T> {
    public:
      void Init(T small) { cout<<"Derived Init: "<< small <<endl; }
    };
    
    int
    main()
    {
      Interface<bool> *i = new Derived<bool>;
    
      i->Init(true);
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM