I have created a class in Qt that has a number of public properties lets say
class items
{
public:
QString name;
QString description;
How can I create a new class that inherits all the variables and methods from this class?
Your question is about c++ not Qt. You can search c++ documents to learn about c++ inheritance.
You can inherit another class in this way:
class myClass : public items
{
myClass() : Item()
{
//
}
}
Related
Is there any way to avoid diamond problem form built in a class where I can't inherit virtually like below?
class Widget
{
// Built in class can't be modified
public:
bool draw();
};
class Button : public Widget
{
// Built in class can't be modified
// Not able to put virtual inheritance for Widget Class
};
class MyOwnSpecialWidget : public Widget
{
// Some special treatment for non container widgets
};
class CustomButton : public Button, public MyOwnSpecialWidget
{
// So here the copy of Widget is coming form both Button
// and MyOwnSpecialWidget class.
};
So, what is the way to get only one copy of Widget class in CustomButton class?
I have 3 classes that derive from one another - GameScreen is the base class to which MenuScreen is derived from. I then have a third class 'TitleScreen' which derives from 'MenuScreen'.
The flow is basically from the base class: 'GameScreen' -> 'MenuScreen' -> 'TitleScreen'
The base class 'GameScreen' has no parameters in it's constructor, like wise with 'TitleScreen', however I need a parameter for 'MenuScreen'. I currently have the header files as:
GameScreen.h
class GameScreen
{
public:
GameScreen();
}
MenuScreen.h
class MenuScreen : public GameScreen
{
public:
MenuScreen(std::string title);
}
TitleScreen.h
class TitleScreen : public MenuScreen
{
public:
TitleScreen(std::string title) : MenuScreen(title);
}
What I'm having difficulty trying to understand is if this is possible in C++ (I'm following a C# sample for Game State Management which does this). Reading through class inheritance in some books I have it only covers parameters inherited from the base class, were as my sample base class has no parameters.
You are missing ; after each class declaration.
If you write TitleScreen(std::string title) : MenuScreen(title) you are defining the body of the method but the body is missing... so you should put just declaration to your TitleScreen.h :
class TitleScreen : public MenuScreen
{
public:
TitleScreen(std::string title);
};
and then place the body of the constructor to TitleScreen.cpp:
#include "TitleScreen.h"
TitleScreen::TitleScreen(std::string title) : MenuScreen(title)
{
// ..
}
Edit: fixed the terminology accordint to this question.
I have a class I would like to use the builder pattern on, but it is derived from a base class whose attributes I need to access. I can't access the members of BaseClass in my implementation without making them public, even if I derive Builder from BaseClass or something equally smelly. My classes:
BaseClass.h:
class BaseClass
{
protected:
CString name;
}
DerivedClass.h:
class DerivedClass : public BaseClass
{
public:
// builder starts here and has the same base class as the class it is nested in
// (if it doesn't, we can't get access to name)
static class Builder : public BaseClass
{
public:
Builder::Builder(CString pName)
{
this->name = pName;
}
Builder Builder::Description(CString pDescription)
{
this->description = pDescription;
return *this;
}
};
public:
CString description;
};
DerivedClass.cpp:
DerivedClass::DerivedClass(Builder builder)
{
this->name = builder.name; // this is NOT ok
this->description = builder.description; // this is ok
}
My problem is that I can't access builder.name. Visual Studio says that "protected member BaseClass::name is not accessible through a DerivedClass::Builder pointer or object". I've tried fiddling around with making Builder a friend of BaseClass, but to no avail. This post has also provided a workaround, but it's for Java, and very messy.
Is there a decent way to use the builder pattern with inheritance in C++?
Even though Builder is declared inside of DerivedClass, Builder is not implicitly friends with DerivedClass, like you are expecting. Builder is still its own class, and it follows the same rules as any other class, including scope access rules. That is why DerivedClass cannot access protected members of Builder by default. You need to explicitly declare that friendship.
Also, your Builder::Description() method will not work as-is because Builder does not have a description member. The this pointer inside of Builder methods still refers to the Builder instance, not a DerivedClass instance. If you want the Builder to access members of DerivedClass, it needs to be given a pointer to a DerivedClass instance. Otherwise, give Builder its own description member (which it looks like you were attempting to do anyway).
Try this:
BaseClass.h:
class BaseClass
{
protected:
CString name;
};
DerivedClass.h:
class DerivedClass : public BaseClass
{
public:
class Builder : public BaseClass
{
public:
Builder(const CString &pName)
{
this->name = pName;
}
Builder& Description(const CString &pDescription)
{
this->description = pDescription;
return *this;
}
public:
CString description; // <-- add this
friend class DerivedClass; // <-- add this
};
public:
DerivedClass(const Builder &builder);
public:
CString description;
};
DerivedClass.cpp:
DerivedClass::DerivedClass(const DerivedClass::Builder &builder)
{
this->name = builder.name; // this is ok now
this->description = builder.description; // this is ok now
}
Don't attempt to access the member directly. Use a public accessor method.
I want to extract common code from a few WinRT components to one base class so I don't need to copy&past it. I have the following base class:
[Windows::Foundation::Metadata::WebHostHidden]
ref class ExpandableView : public Windows::UI::Xaml::DependencyObject
{
public:
static void onIsExpandedChanged(Windows::UI::Xaml::DependencyObject^ object,
Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ arguments);
public:
property bool IsExpanded
{
bool get(){return (bool)GetValue(IsExpandedProperty);}
void set(bool value){SetValue(IsExpandedProperty, value);}
}
static property Windows::UI::Xaml::DependencyProperty^ IsExpandedProperty
{
Windows::UI::Xaml::DependencyProperty^ get(){return _IsExpandedProperty;}
}
protected:
ExpandableView();
virtual void viewExpanded();
virtual void viewCollapsed();
private:
void _expand();
void _collapse();
private:
static Windows::UI::Xaml::DependencyProperty^ _IsExpandedProperty;
};
And I create a few User Controls which should be somehow inherited from this base class. And it is not possible to do it the way I want because winrt class can inherit only one ref class and other should be interfaces. But I need this very class which has dependency property which has some logic when it is set and I don't want to copy&past this property across all my classes.
So the question is: how to achieve it with WinRT?
Have you tried using a template and inheritance of the specific class needed:
template<typename BaseClass>
ref class ExpandableView : public BaseClass;
Now the subclasses reusing ExpandableView can inherit whatever they need, not only Windows::UI::Xaml::DependencyObject.
I heard that it is better to separate data and GUI. For examples, I have some data. It is hierarchical with abstract base and derived class for concrete types, like
class Data {};
class ConcreteDataA : public Data {};
class ConcreteDataB : public Data {};
And I also have its hierarchical GUI (for example dialog)
class DataDialog {};
class ConcreteDataADialog : public DataDialog {};
class ConcreteDataBDilaog : public DataDialog {};
And I want create a data dialog object from a data object. If the data object is ConcreteDataA, ConcreteDataADialog is created, if B, B dialog is created. There is an easy way to do it by adding a virtual function in class Data like
virtual DataDialog* CreateDialog()
But if I add this function in the data class. it seems to violate the data/GUI separation principle. The second way is to build a global CreateDialog function, and create dialogs according to the dynamic_cast type of data object. This way is also not good for many maual ifs. Any other way to implement it? Or in practice, the first way is also okay? Thanks a lot!
One of my friends told me to use reflection. I think this should work.
It seems that you're looking for an Abstract Factory.
An Abstract Factory is a design pattern in which different types of objects can be created depending on the argument. So in this example, the factory will create a ConcreteDataADialog or a ConcreteDataBDilaog depending on the type of the data.
Code sketch:
class DialogFactory {
public:
virtual Dialog* createDialog() = 0;
};
class ADialogFactory : public DialogFactory {
public:
Dialog* createDialog() {
return new ADialog();
}
};
class BDialogFactory : public DialogFactory {
public:
Dialog* createDialog() {
return new BDialog();
}
};
class Application {
Dialog* createSpecificDialog(Data data) {
if (data.isA()) {
return new ADialogFactory().createDialog();
} else {
return new BDialogFactory().createDialog();
}
}