Calling a QMainWindow method in its instance class method - c++

I have a MainWindow class
class MainWindow : public QMainWindow
{
customClass * obj;
public:
void foo(bool);
}
Here is my customClass:
class customClass
{
void foo1(bool);
}
Now, I want to call foo() method in foo1().
How to do that?

You can make the constructor of your customClass take a pointer to a MainWindow which it stores in a member variable for later use.
class customClass
{
public:
customClass(MainWindow* mainWindow)
: mainWindow_(mainWindow)
{
}
void foo1(bool b) {
mainWindow_->foo(b);
}
private:
MainWindow* mainWindow_;
}

One of way - using of dependency injection pattern: link
struct A;
struct B
{
B( A& a );
void foo1()
{
m_a.foo();
}
private:
A& m_a;
}
struct A
{
void foo(){}
B m_b;
}

You can make your MainWindow implements singleton pattern (if it's applicable to your design), then you can directly get an instance from any place you like.

Related

How to create uniform interface to build for builders in builder patterns, if one of concrete builders take extra argument?

class Builder
{
public:
virtual void Build(int a) = 0;
};
class ConcreteBuilder1 : public Builder
{
public:
void Build(int a);
};
class ConcreteBuilder2 : public Builder
{
public:
void Build(int a. struct A* a);
};
So My question is how to design Build() method to take both parameters(int, struct* A). But with same builder interface ?
So that,
int x;
struct A* y;
Builder concrteBuilder1 = new ConcreteBuilder1();
concrteBuilder1 ->Build() // // Here I am forced to pass struct A* a eventhough not needed for concerte builder1 . And I am also forced to forced to change Builder interface too.
My apologies if I did not convey the question clearly.
Builder usually builds an object of another class. You may try something similar to this:
class ToBuild
{
//some code here
};
class Builder
{
public:
virtual ToBuild * build() = 0;
};
class ConcreteBuilder : public Builder
{
int _valA;
int _valB;
public:
ToBuild * build() override
{
ToBuild * obj = new ToBuild();
//initalize obj using _valA and _valB variables;
return obj;
}
ConcreteBuilder& valA(const int val)
{
_valA = val;
return *this;
}
ConcreteBuilder& valB(const int val)
{
_valB = val;
return *this;
}
};
int main()
{
ConcreteBuilder b;
ToBuild * obj = b.valA(1).valB(2).build();
//some code
delete obj;
return 0;
}
[edit]
You can write another derived class with as many parameters as you want and still use a single 'build' method.
Add
using Builder::Build;
to the derived class's declarations. This will import this symbol into the derived class, and make both it, and the derived class methods, of the same name, available to overload resolution. I.e.
using Builder::Build;
void Build(int a);
I'm ignoring the fact that you can't construct the derived class anyway, since it fails to implement this pure virtual function from the base class.
Derived class override their functions from the one which has same parameters and same return value.
So if you declare a function like...
class Builder
{
public:
virtual void Build() = 0;
};
class ConcreteBuilder1 : public Builder
{
public:
void Build(int a);
};
class ConcreteBuilder2 : public Builder
{
public:
void Build(int a. struct A* a);
};
Here, Build function in ConcreteBuilder1 class treated as a new function, not override function.
So try this.
class Builder
{
public:
virtual void Build(int a, A* b = 0) = 0;
};
class ConcreteBuilder1 : public Builder
{
public:
void Build(int a);
};
class ConcreteBuilder2 : public Builder
{
public:
void Build(int a, struct A* a);
};
I wish it's helpful for you.

Automate window programming through a base class

I have programmed several windows for an application that all inherit Gtkmm::Window. At this point, I would like to automate the process. Right now, the following structure stands out:
class MyWindow : public Gtk::Window
{
public:
MyWindow();
virtual ~MyWindow();
//...
private:
void registerLayouts(); // Adds layouts to the window.
void registerWidgets(); // Adds widgets to the layouts.
//...
};
And the constructor:
MyWindow::MyWindow()
{
registerLayouts(); // Cannot be virtual: in constructor.
registerWidgets(); // Cannot be virtual: in constructor.
//...
}
So the problem is that all of this has to be done manually (i.e. copy/pasted) every time a new window has to be programmed because registerLayouts() and registerWidgets() are called at construction and hence cannot be virtual.
Ideally, I would have a base class that I could inherit from which would give me the option of overriding the two methods and would take care of the rest: it would call the two methods at an appropriate location.
The thing is, I have not found where this appropriate location could be. I have look at different signal handlers, but there seem to be none for this.
Do you have an idea of how I could do this?
MFC has the CDialog::OnInitDialog() that performs something similar to what I need.
You could delegate the work to a separate class:
class MyWindow : public Gtk::Window
{
//public: *** EDIT ***
protected:
template <typename LayoutManager>
MyWindow(LayoutManager const& lm)
{
lm.registerLayouts(this);
lm.registerWidgets(this);
}
};
class SubWindow : public MyWindow
{
class LM { /* ... */ };
public:
SubWindow() : MyWindow(LM()) { }
};
(Edited: The improved pattern hides away from public the layout managers of sub classes...)
Alternatively, the whole class as template (possibly superior to above):
template <typename LayoutManager>
class MyWindow : public Gtk::Window
{
public:
MyWindow()
{
LayoutManager lm(*this);
lm.registerLayouts();
lm.registerWidgets();
}
};
class SpecificLayoutManager { /* ... */ };
using SpecificWindow = MyWindow<SpecificLayoutManager>;
If you need the layout manager for cleaning up as well (not familiar with GTK myself...):
template <typename LayoutManager>
class MyWindow : public Gtk::Window
{
LayoutManager lm;
public:
MyWindow() : lm(*this)
{
lm.registerLayouts();
lm.registerWidgets();
}
virtual ~MyWindow()
{
// still access to lm...
}
};
Important side note: In all variants we do not yet have a fully constructed derived class – casting to the latter within the layout managers thus is not legal (experimented with curiously recurring template pattern, but dropped the idea for exactly the same reason: needed to cast to derived in constructor of base as well).
Edit in response to comments: A sample on how you could manage additional members of a subclass (using third variant above, the template class one with the layout manager member; lm member now needs to be protected):
class SubWindowLayoutManager
{
template <typename>
friend class MyWindow;
friend class SubWindow;
int someMember;
void registerLayouts() { }
void registerWidgets() { }
};
class SubWindow : public MyWindow<SubWindowLayoutManager>
{
void doSomething()
{
lm.someMember = 77;
}
};
Additionally a new variant entirely without templates:
class MyWindow : public Gtk::Window
{
protected:
class LayoutManager
{
public:
virtual void registerLayouts(MyWindow* parent) = 0;
virtual void registerWidgets(MyWindow* parent) = 0;
};
std::unique_ptr<LayoutManager> lm;
MyWindow(std::unique_ptr<LayoutManager> lm)
: lm(std::move(lm))
{
this->lm->registerLayouts(this);
this->lm->registerWidgets(this);
}
};
class SubWindow : public MyWindow
{
class LM : public LayoutManager
{
public:
void registerLayouts(MyWindow* parent) override { }
void registerWidgets(MyWindow* parent) override { }
int someMember;
};
// convenience access function:
inline LM& lm()
{
return *static_cast<LM*>(MyWindow::lm.get());
}
public:
SubWindow() : MyWindow(std::make_unique<LM>()) { }
void doSomething()
{
//static_cast<LM*>(lm.get())->someMember = 77;
lm().someMember = 77;
}
};

c++ how to use mocks instead of normal class

class MyClass
{
public:
MyClass()
{
otherClass = new ConcreteClass();
}
private:
void FuncA()
{
otherClass->FuncB();
}
IOtherClass* otherClass;
};
How should I redesign my code to use mock derived from IOtherClass instead of ConcreteClass? Suppose that I cannot change FuncA().

Call a method from A class in constructor of other class

I want to call a method from A class in constructor of other class
I googled, but did not find any answer
For example, I have :
class A{
void doWork();
}
class B{
B(){
//here i want to have doWork method
}
}
You told us not enough to choose proper solution. Everything depends on what you are trying to achieve. A few solutions:
a) Mark A method as static.
class A
{
public:
static void DoSth()
{
// Cannot access non-static A members here!
}
};
class B
{
public:
B()
{
A::DoSth();
}
};
b) You can instantiate A in place
class A
{
public:
void DoSth()
{
// Do something
}
};
class B
{
public:
B()
{
A a;
a.DoSth();
}
};
c) You can put A's instance into B:
// A remains as in b)
class B
{
private:
A a;
// or: A * a;
public:
B()
{
a.DoSth();
// or: a = new A; a->DoSth();
// Remember to free a somewhere
// (probably in destructor)
}
}
d) You may derive B from A:
class A
{
protected:
void DoSth()
{
}
};
class B : public A
{
public:
B()
{
DoSth();
}
};
e) You can forget about A class and make DoSth a function:
void DoSth()
{
// ...
}
class B
{
public:
B()
{
DoSth();
}
}
Since you provided not enough data, you have to choose solution on your own.
In order for that to work you'd need to subclass it.
So it'd be like this:
class A {
doWork();
}
class B : A {
B(){
doWork();
}
}
You could also do it like so going for a HAS-A rather than IS-A relationship:
class A {
doWork();
}
class B {
A myA;
B(){
myA.doWork();
}
}
Without knowing more of what you are doing I'd go with the top (IS-A) solution which is what I think you are trying to do.
Or
class A
{
public:
static void doWork();
};
class B
{
B(void)
{
A::doWork();
}
};
?
PS: Here B::B() will be private

How can I pass a variable from class element to class element? (C++)

My question might not be too correct... What I mean is:
class MyClass
{
public:
MyClass()
{
}
virtual void Event()
{
}
};
class FirstClass : public MyClass
{
string a; // I'm not even sure where to declare this...
public:
FirstClass()
{
}
virtual void Event()
{
a = "Hello"; // This is the variable that I wish to pass to the other class.
}
};
class SecondClass : public MyClass
{
public:
SecondClass()
{
}
virtual void Event()
{
if (a == "Hello")
cout << "This is what I wanted.";
}
};
I hope that this makes at least a little sense...
Edit: _This changed to a.
What you need to do is make SecondClass inherit from FirstClass and declare _This as protected.
class FirstClass : public MyClass
{
protected:
string _This;
public:
and
class SecondClass : public FirstClass
What you got doesn't make sense because classes can only see members and functions from their parents (MyClass in your case). Just because two class inherit from the same parent does not mean they have any relation or know anything about each other.
Also, protected means that all classes that inherit from this class will be able to see its members, but nobody else.
I guess that you need something like this (for a sake of simplicity, I've omitted all the unnecessary code):
class Base{
public:
~Base(){}
protected:
static int m_shared;
};
int Base::m_shared = -1;
class A : public Base{
public:
void Event(){
m_shared = 0;
}
};
class B : public Base{
public:
void Event(){
if (m_shared == 0) {
m_shared = 1;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
a.Event();
b.Event();
return 0;
}
To explain above, I'll explain the static data members:
Non-static members are unique per class instance and you can't share them between class instances. On the other side, static members are shared by all instances of the class.
p.s. I suggest that you read this book (especially Observer pattern). Also note that above code is not thread-safe.