Every time I create a class, there is no destructor in the body of the class? Why is that? The Visual Studio will create a destructor automaticly but the Qt creator doesn't. Is there something special in the class of Qt?
In most well-designed C++ classes you don't need any custom code in the destructor and the default behavior is the right thing (i.e. destroying all members).
You can add a destructor if you want of course and please note that QtCreator mitigates the C++ annoyance of header/implementation duality by allowing automatic creation of the declaration from the implementation or vice versa (e.g. context menu -> refactor -> add public declaration).
Related
I ended up in a situation lined out below.
I have one library that is pure CPP without external libraries, and another project that is an SDK to interface with an external library.
"I" in front of the name indicates an abstract class. Arrows indicate inheritance.
I have IDevice which contains HandleInput(data) as a callback, and StartDevice().
Then I have a more specific type of device: ISmartwatch (containing StartTimer), and from that inherits a more specific version SmartwatchV1, which implements HandleInput(data) according to its needs.
That all seemed great until I came to the external SDK part, where the library expects me to use inheritance to interface with it to override some functions. So, I have to inherit from the external library, and from my own CPP library, to override the functions I need. Most of these library overrides suit any device (IExternalLibDevice), but a few are specific to the exact Stopwatch version (ExternallSmartWatchV1).
Then for polymorphism in my SDK, I would like to call and override functions both provided by the library and my own device example: libDevice.StartDevice() and use library calls within this optionally overriden StartDevice. Or stopWatch.StartTimer(), stopwatchV1.libraryOverride().
The object which I need to create is the green one, however, the white SmartWatchV1 is also an object to instantiate in applications without the library. (And obviously I keep in mind any future alternative devices or stopwatch versions.)
I think if I drop any inheritance arrow, I would either lose out on polymorphism (so SDK code will only work for a very specific smartwatch version), or I cannot override functions I need anymore. Composition would be nice, but won't work for overriding functions, or is there an option I don't know about?
And so, I ended up here. I am encountering quite some annoying errors implementing this, since double diamond is usually solved with virtual inheritance (nice page about double diamond: https://isocpp.org/wiki/faq/multiple-inheritance#mi-diamond). However, when applied here (see the v's that indicate "virtual" in the image), I have one inheritance that should both be virtual and not be virtual. Additionally, virtual inheritance makes constructors really annoying in my generic CPP library. Even without virtual (which as far as I'm aware would cause some duplication of classes in memory and a lot of ambiguity to solve), I have some constructor errors ("no suitable default constructor" for a class that must not have a default constructor, etc) issues.
I have been battling to solve this for a long time, and I hope someone more experienced can make a suggestion that provides a better solution for my code structure or issue.
In the end, I solved it by using composition:
Add an IDevice pointer to IExternalLibDevice that is set in the constructor.
In IExternalLibSmartwatch: add an ISmartwatch pointer to the constructor and pass it to its parent constructor. Also, add a function that retrieves the IDevice pointer as an ISmartwatch.
In ExternalSmartwatchV1: also add a SmartwatchV1 to the constructor and pass it to its parent constructor, and create a function that retrieves the IDevice pointer as a SmartwatchV1.
The IDevice pointer holds the reference to the cppLibDevice, and can now be cast to any of the subclasses it belongs to. Downside: I cannot override the cpp lib classes, but it was not a hard requirement for my code, since I created alternative functions in the ExternalLib classes that can optionally call the cppLibDevice functions, or completely replace them.
So me and my friends are developing Connect4 in C++. At first we elaborated the logic behind the game in a Visual Studio Console Application. We came up with 3 classes, "Logic", "GameUi" (That name is probably not suitable) and "Gui". (I should mention that all members off these classes are static members - so no instances)
Once the logic worked it was my job to tranfer it to Qt. And here's the problem:
So basically once the player has done an input (aka. The Player has chosen a column in which he wants to throw the slice (?) in) the Logic class processes this input and updates the vector in which we store the field. After this Logic calls the GamUi class, which should then call a function in the Gui class (Note that the Gui class is now the Qt class). The Problem with that is that I can't call a non-static function in the Qt class to change the Ui from a static function from a different class.
At first I thought about making the Ui public, which is according to the internet not a good programming exercise.
Thank you very much in advance
Ps: Please don't judge me for my non-native-speaker-english and my not very good c++ skills.
Assuming GUI is a singleton, you might code a static GUI::instance() method that returns a pointer to itself. Call it from anywhere and you have your pointer. Better yet, [inherit from QObject and] use signals and slots.
Some time ago I programmed a GUI with QT Designer / QT Creator.
For this question I will first state a schematic of how the general process of creating a GUI with the mentioned IDE works:
Creating the design with the QT Designer -> Get .ui files
The .ui files are translated into header files and you especially get something like "UIbasisclass.h" (with class UIbasisclass) .
You create something like an "UIsubclass.h" (with class UIsubclass) yourself making one private member UIbasisclass ui.
Code within class UIsubclass:
...
private:
Ui::UIbasisclass ui;
...
Finally you will create an object of UIsubclass in the main method -> Code:
...
UIsubclass *MyGUI = new UIsubclass();
...
where the constructor of UIsubclass consists among other code of:
...
ui.setupUi(this);
...
In short: We have a UIsubclass that is mostly responsible for applicational methods, but also has a private member of UIbasisclass named ui that consists mostly of design code.
When we create an object of UIsubclass its private member UIbasisclass ui is initialized within the constructor of UIsubclass with the object of UIsubclass itself (?). [see: this pointer]
My questions are now:
Why isn't there used inheritance in the way that UIsubclass inherits from UIbasisclass? Instead one object of UIbasisclass becomes member of UIsubclass.
Is this some specific advantageous concept (if yes which advantages has it or how is it named?) or is it "just" a necessity of the QT code structure?
Let me know if I have to specify my questions or if there are any questions.
You can do with private inheritance, it is even documented in Qt documentation.
The use of a private member for ui is the default because of the templates used by Qt Creator, Qt itself does not care.
Why isn't there used inheritance in the way that UIsubclass inherits from UIbasisclass?
You're asking us about why you didn't do it in your own code? Just do it. It's up to you. It truly is your code. You are responsible for its design. If you're using a template, it's there to help you get started, but not to design your software for you. The responsibility is yours, and yours only.
it "just" a necessity of the QT code structure?
There is no such necessity. The Ui class is a POD class with a bunch of pointers and one method. Nothing much to it. If you want to privately inherit from it: go right ahead.
Because with a private member you can forward declare the generated class:
namespace Ui {
class MyForm;
}
class Foo {
private:
Ui::MyForm *ui;
};
and on the .cpp file you insert the include.
this way all of the possible includes of this file will not have to preprocess that file again.
I am using QT4.8.4 + Qt Creator 2.8.1. Now I need to create several classes Child_X that inherit from another class Parent. In Parent I have several virtual methods.
Now I have to implement them in all of my Child_X classes. To save editing time, I'd like Qt do that for me automatically. When I remember right there is the possibility to have Qt create all the virtual methods. Does anybody know how?
Thank you
Sorry, I did not formulate correctly: I did not mean that Qt will automatically write the body of the methods. ( To invent that would probably make you very rich :-) )
I was talking about Qt writing all the headers of the virtual methods in the newly created (inherited) class. This saves a lot of writing/copying classnames etc.. The body would be empty in all the virtual methods.
Thank you
itelly
I hope you already found it, but maybe for others:
Right click on the class name in the editor.
In the menu, click on 'Refactor' and then on 'Insert virtual functions of base classes'.
You can choose to directly make the functions in the implementation file (as well as in the header file).
there is the possibility to have QT create all the virtual methods
There is no such possibility, because Qt can't read your mind and divine what those implementations are supposed to do.
Speaking of that - what are your virtual methods supposed to do? Please edit the question to fix that.
i have been going through the source code of an application which i downloaded from sourceforge.
i have a certain method which requires a pointer to an abstract class as a parameter.
class A;//abstract class
B::method(A *)
i cant create an object of A since its abstract.
so i can only pass pointer to object of child class of A in the B::method().
Now the problem is there are many classes in the source.
my problem is that how do i know which are the child classes of a parent class in visual studio 2010?
i have tried "find all references" for a virtual method of the abstract class (because its definition should be in the child class!) but without luck.
hope i could make my question clear!
Maybe "Solution Explorer" -> Your project -> Right Click Menu -> "Class Diagram" is what you're looking for ?
Another suggestion - try looking for " : public YourBaseClassName" strings and then "unwind" the inheritance. The usability certainly depends on the depth of your hierarchy. If there are multiple intermediate classes or multiple inheritance, then... Maybe, look at Eclipse+CDT ?