Where do I define a struct in order to be accessible to all my classes?



  • The files I have are the following:

    • myProject.pro
    • mainwindow.h
    • otherwindow.h
    • main.cpp
    • mainwindow.cpp
    • otherwindow.cpp
    • mainwindow.ui
    • otherwindow.ui

    In which one should I put the struct definition?


  • Moderators

    Hi, welcome to the forum.

    There are two things here - the declaration and an instance of that struct.
    The declaration should go in neither of these files. It should go into myawesomestruct.h, and get included in .cpp files you need it in.
    As for an instance of that struct - that depends on what that struct is and how it should be interacted with.

    It can be a global instance - declared as extern in a header and defined in its own .cpp file.
    It can be a member of a globally accessible instance of some class.
    It can be a dynamically added property on a globally accesible object (e.g. the application object).
    It can be a member passed as a parameter between other classes methods.
    It can be a member of a class which instance is passed around.
    ... <- enter 10 other options here

    It all depends on the purpose of that struct and intended usage pattern.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.