jeudi 19 avril 2018

C++/(Qt) pointer to any object with specific method

How do I do to call a specific method present on all classes from an other (TranslationManager) class ? I simplified a lot the code. I just want to call the setTranslationText of any class from TranslationManager.

These are to take in consideration:

  1. All classes have a setTranslationText method
  2. We should call setTranslationText of any class from TranslationManager by using the pointer to the class

    class Interface
    {
       ...
       public:
         void setTranslationText(QString translatedString); 
    }
    
    class AnyOtherInterface
    {
       ...
       public:
         void setTranslationText(QString translatedString); 
    }
    
    ...
    
    …
    Translationmanager::Translationmanager(){
       AnyClass = Interface; // Pointer to Interface Class
       AnyClass->setTranslatioNText("Text");
    
       AnyClass = AnyOtherInterface; // Pointer to AnyOtherInterface Class
       AnyClass->setTranslatioNText("AnotherText");
    }
    
    
    …
    
    

Aucun commentaire:

Enregistrer un commentaire