-2

I want to share value initialize from Application object to child without pass any parameter. ex, i need to pass mHInstance to Window, Button, Label class. How to do this?

Application->Widget->Window. Application->Widget->Button. Application->Widget->Label.

Header

#include <Windows.h>

class Application
{
  protected:
    HINSTANCE mHInstance;

  public:
    Application(HINSTANCE);
};

class Widget: public Application
{
  protected:
    HWND mHWnd;
  
  public:
    void show();
};

class Window: public Widget
{
  public:
    Window();
};

Implementations

Application::Application(HINSTANCE hInstance)
{
  mHInstance = hInstance;
};

// Window implementations

Window::Window()
{
  WNDCLASSEX wc = {0};

  // Access application member that already initialize
  wc.hInstance = mHInstance;

  RegisterClassExW(&wc);
};

Main

#include <Windows.h>
#include "../headers/gui.h"

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PWSTR lpCmdLine, int nCmdShow) {

    Application app(hInstance);

    Window* wnd = new Window;
    wnd->show();

    return app.exec();
}
david
  • 3
  • 4
  • in what universe widget is a subclass of application? If anything, application is a god superclass of any widgets – Swift - Friday Pie Oct 22 '21 at 10:28
  • I think I didn't get exactly what you want to know... child classes inherit from parent classes. As long you do not use private, child classes have access to the parameters of the parent class. – MiniMik Oct 22 '21 at 10:28
  • I'm not quite sure I understood but if your `Widget` class inherits from `Application` it must be able to call `Application's` constructor (non-default) with some value. The compiler will not generate a default ctor for Widget in your case. – ben10 Oct 22 '21 at 10:32
  • @MiniMik i know i can access member when make definition of class. what i want is access member that initialize first when create object then use it in background. not by pass parameter. – david Oct 22 '21 at 10:33
  • 1
    @sorosh_sabz hmm, i dont know, i can do like this. i will try it – david Oct 22 '21 at 10:34
  • @david I cannot edit my comment, my comment is incorrect, please see my answer. – sorosh_sabz Oct 22 '21 at 10:50
  • "A widget is a special kind of application and a window is a special kind of widget" is a pretty odd idea. More commonly, an application *has* windows, and windows *contain* widgets. – molbdnilo Oct 22 '21 at 11:02

1 Answers1

0

You can simply change your main like below

#include <Windows.h>
#include "../headers/gui.h"

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PWSTR lpCmdLine, int nCmdShow) {

    Application* app = new Window(hInstance);

    Window* wnd = static_cast<Window*>(app);
    wnd->show();

    return app->exec();
}

And for inheriting constructor you must change classes like below

#include <Windows.h>

class Application
{
  protected:
    HINSTANCE mHInstance;

  public:
    Application(HINSTANCE);
};

class Widget: public Application
{
  protected:
    HWND mHWnd;
  
  public:
    using Application::Application;
    void show();
};

class Window: public Widget
{
  public:
    using Widget::Widget;
    Window();
};

note: make sure your compiler at least support C++11

Dharman
  • 30,962
  • 25
  • 85
  • 135
sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53