Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to solve the problem of inherits multi super classes

How to solve the problem of inherits multi super classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 7 Posters 797 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nicker playerN Offline
    nicker playerN Offline
    nicker player
    wrote on last edited by
    #1
    
    
    //DEFINED THE INTERFACE OF THE CLASS
    class ICallback{
    private:
    		ICallback * m_callback;
    public:
    	void setCallback(ICallback * ptr){
    		m_callback = ptr;
    	}
    	ICallback * getCallback(){
    		return m_callback;
    	}
    	virtual void callback(void * ptr){
    		...
    	}
    	
    	QString dosomething(){
    		...
    		//return the marked string of the ICallback 
    		...
    	}
    }
    
    //
    class ClassA :public QPushButton,public ICallback{
    Q_OBJECT
    public:
    	virtual void callback(void * ptr) override{
    		...
    		//do some thing marked the ClassA for dosomething() could get a value of the ClassA
    		...
    		ICallback * t_callback = getCallback();
    		//here is the problem,t_callback->callback called but the pointer of this seemed cant find the real object
    		t_callback->callback(this);
    	}
    	
    }
    
    class ClassB :public QWidget,public ICallback{
    Q_OBJECT
    public:
    	virtual void callback(void * ptr) override{
    		...
    		QObject * t_obj = (QObject*)ptr;
    		// this code could run normally
    		t_obj->objectName();
    		//
    		ClassA * t_classa = (ClassA*)ptr;
    		
    		//this is the problem,the t_classa
    		t_classa->dosomething();
    		
    	}
    }
    
    
    //usage :
    
    ClassA * ptrA = new ClassA();
    
    ClassB * ptrB = new ClassB();
    
    ptrA->setCallback(ptrB);
    //here is the problem, the ClassB callback(){} could not run the code t_classa->dosomething(),caused the pointer 
    ptrA->callback();
    
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • nicker playerN nicker player
      
      
      //DEFINED THE INTERFACE OF THE CLASS
      class ICallback{
      private:
      		ICallback * m_callback;
      public:
      	void setCallback(ICallback * ptr){
      		m_callback = ptr;
      	}
      	ICallback * getCallback(){
      		return m_callback;
      	}
      	virtual void callback(void * ptr){
      		...
      	}
      	
      	QString dosomething(){
      		...
      		//return the marked string of the ICallback 
      		...
      	}
      }
      
      //
      class ClassA :public QPushButton,public ICallback{
      Q_OBJECT
      public:
      	virtual void callback(void * ptr) override{
      		...
      		//do some thing marked the ClassA for dosomething() could get a value of the ClassA
      		...
      		ICallback * t_callback = getCallback();
      		//here is the problem,t_callback->callback called but the pointer of this seemed cant find the real object
      		t_callback->callback(this);
      	}
      	
      }
      
      class ClassB :public QWidget,public ICallback{
      Q_OBJECT
      public:
      	virtual void callback(void * ptr) override{
      		...
      		QObject * t_obj = (QObject*)ptr;
      		// this code could run normally
      		t_obj->objectName();
      		//
      		ClassA * t_classa = (ClassA*)ptr;
      		
      		//this is the problem,the t_classa
      		t_classa->dosomething();
      		
      	}
      }
      
      
      //usage :
      
      ClassA * ptrA = new ClassA();
      
      ClassB * ptrB = new ClassB();
      
      ptrA->setCallback(ptrB);
      //here is the problem, the ClassB callback(){} could not run the code t_classa->dosomething(),caused the pointer 
      ptrA->callback();
      
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Can you please describe your problem and what you're trying to achieve. Your code above does not tell me anything.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      nicker playerN 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Can you please describe your problem and what you're trying to achieve. Your code above does not tell me anything.

        nicker playerN Offline
        nicker playerN Offline
        nicker player
        wrote on last edited by nicker player
        #3

        @Christian-Ehrlicher

        class ClassB
        virtual void callback(void * ptr) override{
                          //this is the problem,the t_classa dosomething() dosnt work. I trans the pointer of the void  to the ClassA,But it failed
        		t_classa->dosomething();
        }
        
        W J.HilkJ 2 Replies Last reply
        0
        • nicker playerN nicker player

          @Christian-Ehrlicher

          class ClassB
          virtual void callback(void * ptr) override{
                            //this is the problem,the t_classa dosomething() dosnt work. I trans the pointer of the void  to the ClassA,But it failed
          		t_classa->dosomething();
          }
          
          W Offline
          W Offline
          wrosecrans
          wrote on last edited by
          #4

          @nicker-player "failed" how? You aren't actually explaining what problem you are having, or what you are trying to accomplish.

          1 Reply Last reply
          0
          • nicker playerN nicker player

            @Christian-Ehrlicher

            class ClassB
            virtual void callback(void * ptr) override{
                              //this is the problem,the t_classa dosomething() dosnt work. I trans the pointer of the void  to the ClassA,But it failed
            		t_classa->dosomething();
            }
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @nicker-player first of all, for the love of the c++ gods, don't use c-style casts, use dynamic cast here, because I'm betting my last pair of underwear that you did not disable RTTI. So it is available to you.

            post what you actually do in "do something"

            also I fail to see your initialisation of ICallback where do you do that ?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              Your callback inside ClassB casts the pointer ptr it receives to ClassA. For a callback I would expect it to be called with itself as parameter. So, if ptr is of type ClassB you are not allowed to cast it to ClassA because those are not related at all. Is ptr of type ClassA in your case?

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Hitul
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by
                  #8

                  How to solve the problem of inherits multi super classes

                  In computer science texts this is called the "diamond problem". There are countless online discussions about it. It's not specifically a Qt problem, but a design paradigm challenge.

                  If you meet the AI on the road, kill it.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved