2

I'm not very good at C++ so prepare for improper use of terms.

Basically I want to gather a bunch of functions inside a sub class of another class, so I would interface with it kinda like this:

mainWindow.add.menubar();
            ^- this is the part I don't know how to do

My class looks something like this at the moment:

namespace GUI {
    class Window {
    public:
        std::string title = "Empty Title";
        int show();
        // Using a struct didn't work but it's what I have at the moment.
        struct add {
            int menubar();
        };
    };
}

Obviously I could simply use mainWindow.addMenubar() but it would be much nicer to add it to a subclass (sub object? I don't know, I'm more used to Javascript programming).

And yes, I'm basically creating my own GUI framework with insufficient C++ expertise, I know it's a bad idea, but it didn't stop me from modifying the Linux kernel to allow me to install Nethunter on my Samsung S4 and it's not gonna stop me now.

Simon Hyll
  • 3,265
  • 3
  • 24
  • 44
  • 4
    You don't, yet, have a sub-object. You have a nested type (`add`), but you aren't creating any instances (objects) of it, and trying to invoke non-static method on a type. – Algirdas Preidžius Jan 09 '18 at 18:08
  • 1
    To call `Window::add::menubar` you need to create an instance of `Window::add`. This instance could live inside `Window` if you'd like. – AndyG Jan 09 '18 at 18:08
  • 3
    Looks like you could use [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Jan 09 '18 at 18:09
  • 1
    Note that nested struct definition can be private, while the instance of it should be public. – Nemanja Boric Jan 09 '18 at 18:13
  • 1
    Why do you want to do that? –  Jan 09 '18 at 18:14

2 Answers2

5

You can inject Window* pointer to the struct Add() constructor, something like:

namespace GUI {
    class Window {
    public:
        std::string title = "Empty Title";
        Add add;     // <- NOTICE: this is an instance of struct Add
                     // which holds the pointer to the window you want 
                     // to draw on
    public:
        Window() : add{this} {}
        int show();
        // Using a struct didn't work but it's what I have at the moment.
        struct Add {
            Window* win;
            Add(Window* w) : win{w} {}
            int menubar() {
                //  here you can use win to draw the puppy :)
            }
        };
    };
}

and then use it like

Widow w; 
w.add.menubar();

Of course, you could do more styling here (for the real world code): separate declaration from definition via .h/.cpp files, hide the data you dont want to expose with private, declare Add as a friend class etc.

StPiere
  • 4,113
  • 15
  • 24
2

For it to work, add must be a variable:

#include <string>

namespace GUI {
    class Window {
    public:
        std::string title = "Empty Title";
        int show();
        // Using a struct didn't work but it's what I have at the moment.
        struct Add {
            int menubar();
        };
        Add add;
    };
}