Create an Abstract Object and use it without inherit
-
Hi all, I had developed android by java before,
hence I got some confused when I write Qt (C++) now.Here is the situation I met...
I had created a class that handle Rs232 , which inherit QSerialport.
It has functions such as detect port, setting baud rate,read and write port .etc.It looks great and worked well.
But I got a request that have to handle read in data in different way from different place.So...I want the handleData() function in my Rs232 class, which inherit QSerialport, become an abstract function.
And when I used it in mainwindow I don't want to inherit it by mainwindow, I just want to implement the abstract function.Since in C++ I can't use "implement" key word in class like Java....
I got 2 Question :- How to create a class with abstract function, use virtual in front of the function?
- How to use this function in other object, and implement the abstract function customized.
Thanks for reading~ and helping~
-
Hi,
Do you mean a pure abstract class ? If so, no you can't without inheritance.
One possible and simple way would to use signals and slots depending on what you want to do in your function.
Otherwise you could also use a more classic callback function that you set on your custom class.What will you be doing in that abstract function ?
-
Hi @SGaist ,
Thanks for replying.
It's not a pure abstract class.
I actually just have one abstract function in class, which handle the data read from serial port.
Since this function might not be the same in different situation, I want to implement it when I used this class.It seems the signal / slot might be a solution, and it will be more like a Qt style.
//Implement by signal / slot signal: void haveDatatoHandle(QString ReadDate); slots : void readSerialPort(); Void readSerialPort(){ QString str = port->readline(); emit haveDatatoHandle( str ); } // in other class connect(Rs232Port , &Rs232Port ::haveDatatoHandle, this, &AnyClass::whatIwanttodo);
But I still wanna know is it possible to implement a single function like Java do.
-
@Phong An abstract method must be implemented in a class which inherits the class where the abstract method is declared. In C++ an abstract method is actually called "pure virtual method" and looks like this:
class AClass { public: virtual void abstractMethod() = 0; // =0 tells compiler that this method is not implemented in this class, but will be implemented in derived classes };
Please learn C++.
Such a class cannot be instanciated. You need to derive a class from it and implement the abstract method there (you cannot implement a method outside of a class, even not in Java):class BClass : public AClass { public: virtual void abstractMethod() { /* Do here something*/ } } }
"Since this function might not be the same in different situation, I want to implement it when I used this class" - to me this does not make any sense as a method must be implemented in a class not "when I used this class" - this is just not possible even not in Java.
Are you sure inheriting is the way to go for your use case? You could just have one class with different methods and then just use the method you need.
-
Hi @jsulm
Your method is corrected.
As for the usage of my case.
I some times use readLine and sometimes used readAll.
This is because, it got different result, and sometimes there are some issue if I just used one of it.And just like the key_press_listener in Qt, or if you had written android, I want to implement the function like listener,
that I can customize the action when I get the readReady signal from the serial port, BUT not in the serial port class.