Results 1 to 4 of 4

Thread: sharing struct data throughout program

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default sharing struct data throughout program

    I have a struct as a private member of my main Window class, like so:

    Qt Code:
    1. class Window : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. //etc
    6.  
    7. private:
    8. //etc
    9. struct Comp {
    10.  
    11. short dataNUM_TOTc;
    12. char *lyBuName[24];
    13. bool onMA[24];
    14. short dataTYPE[24];
    15. } comp[7];
    To copy to clipboard, switch view to plain text mode 

    The values of the instances of the struct, comp[0-6], are initialized through a member function of the Window class and determine how my QWidgets are set up. I would like to be able to access (read) the values of this struct in the constructor function of another class, defined in another .cpp file. How to do this?

    edit: Actually, I've no reason to make it private, so assume it's public instead of private.
    Last edited by vonCZ; 6th July 2007 at 15:13.

  2. #2
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sharing struct data throughout program

    strange: when I add "curCOMP" and "preCOMP" to the struct:

    Qt Code:
    1. struct Comp {
    2.  
    3.  
    4. short curCOMP; // here
    5. short preCOMP; // and here
    6.  
    7.  
    8. short dataNUM_TOTc;
    9. char *lyBuName[24];
    10. bool onMA[24];
    11. short dataTYPE[24];
    12.  
    13. //alt
    14.  
    15. } comp[7];
    To copy to clipboard, switch view to plain text mode 
    the program crashes. If I change preCOMP from short to int--or if I move both lines below dataTYPE[24] to //alt--it doesn't crash.

    What's going on??

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: sharing struct data throughout program

    Quote Originally Posted by vonCZ View Post
    I would like to be able to access (read) the values of this struct in the constructor function of another class, defined in another .cpp file. How to do this?

    edit: Actually, I've no reason to make it private, so assume it's public instead of private.
    It would be suggested to provide public accessor functions to private data instead of making data itself public. I don't know what's your case but perhaps you could also make accessors and data static (provided that there is always a single Window). This way you wouldn't need a pointer/object to access data.
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    vonCZ (11th July 2007)

  5. #4
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sharing struct data throughout program

    thanx JPN. Here's what i did based on your advice and looking around a bit on the web: first, I removed the "Comp" struct from the "Window" constructor function (above) and have created a new class in separate files:

    dafo.h
    Qt Code:
    1. #ifndef DAFO_H
    2. #define DAFO_H
    3.  
    4. class Dafo
    5. {
    6.  
    7. private:
    8. static short dataNUM_TOTc;
    9. //other variables.......
    10.  
    11. public:
    12. Dafo();
    13. ~Dafo();
    14.  
    15. //GETs
    16. static short get_dataNUM_TOTc() { return dataNUM_TOTc; }
    17.  
    18. //SETs
    19. static void set_dataNUM_TOTc(short value) { dataNUM_TOTc = value; }
    20.  
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    I initialize the private variables in dafo.cpp like so:
    Qt Code:
    1. #include "dafo.h"
    2.  
    3. short Dafo::dataNUM_TOTc = 7;
    4. // other variables
    5.  
    6. Dafo::Dafo(void){
    7. }
    8.  
    9. Dafo::~Dafo(){
    10.  
    11. // delete this;
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    ...and that's it. Now I can access get/set member functions of Dafo from any class-file which includes: #include "dafo.h"

    Anyway, pretty basic C++ I guess, but I thought I would post this in case I've made some obvious C++ programming/protocol mistakes that someone might point out.

    thanks again
    Last edited by vonCZ; 9th July 2007 at 18:38.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 11:12
  2. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 05:19
  3. program for working with Data Base(i need it)
    By banakil in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2007, 23:58
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 13:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.