Results 1 to 5 of 5

Thread: how to share variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    18
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default how to share variables

    in my program, i have 2 cpp & their 2 header files. i want to access variable from one cpp file to other. There is no problem when i access int variable, but when i try to access quint32 variable, its value is not the same which is there in original cpp file. my code is

    //hash.h
    public:
    hashchain(QWidget *parent = 0);
    quint32 initial_key, session_key; //variable originally declared here
    int num;

    //hash.cpp
    void hashchain::display()
    {
    quint32 unum[10];
    int i;
    for(i=0;i<10;i++)
    unum=0;
    ifstream in("session", ios::in|ios::binary);
    in.read((char *)&unum, sizeof(unum));
    initial_key = unum[9];
    session_key = unum[8]; //value session_key defined here

    //comn.h
    #include "hash.h"

    private:
    hashchain *hashch;
    quint32 sessionkey;
    int val;

    //comn.cpp quint32 session_key being accessed from this file
    void comn::display_mac()
    {
    hashch = new hashchain(this);
    sessionkey = hashch->session_key; //declaration here
    val = hashch->num;
    qDebug() << "Session key: " << sessionkey; //value being displayed here is not same as in hash.cpp, above
    qDebug() << "num: " << val;

    }

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: how to share variables

    No disrepect, but:
    This is a Qt related forum, not basic C programming forum, and your question is basic C programming question, and thus off topic.
    You will probably be better off posting such questions on beginners C programming forums.
    The forum "Newbie" here is meant as newbie to Qt, not to C/C++.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2008
    Location
    Norway
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60
    Thanked 2 Times in 2 Posts

    Default Re: how to share variables

    I agree with high_flyer, but I think he should have given you a hint in the right direction. Go get a good book on C++ and read about extern storage class specifier
    !sirius

  4. #4
    Join Date
    Jun 2010
    Posts
    18
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: how to share variables

    Thanx for your advice, but i don't think with extern you can share variable between 2 cpp files with their separate header file. If i am not wrong, extern can be used between 2 cpp files with a common header file.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: how to share variables

    As already mentioned, this is basic C++. There are some good free books online that will explain object oriented programming. You just need to take a little bit of time and read some chapters about it. It's boring, I know, but it's necessary.

    Sharing data between classes is actually very easy.

    Qt Code:
    1. Class1
    2. {
    3. public:
    4. Class1{}
    5. ~Class1{}
    6.  
    7. void doSomething();
    8. int getValue() const;
    9.  
    10. private:
    11. int m_value;
    12. };
    13.  
    14. Class1::Class1()
    15. {
    16. m_value = 1;
    17. }
    18.  
    19. void Class1::doSomething()
    20. {
    21. m_value = 2;
    22. }
    23.  
    24. int Class1::getValue() const
    25. {
    26. return m_value;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Then from anywhere else in the code, you can do:
    Qt Code:
    1. Class1 *myClass1 = new Class1;
    2. myClass1->doSomething();
    3. int theValue = myClass1->getValue();
    4. delete myClass1;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Safe to share a QCompleter?
    By ChrisW67 in forum Qt Programming
    Replies: 7
    Last Post: 6th June 2014, 10:58
  2. Share a variable between forms
    By sepehr in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2009, 07:05
  3. cannot share the database connection!!!!
    By cbarmpar in forum Qt Programming
    Replies: 13
    Last Post: 23rd September 2008, 14:42
  4. What is the best way to share data between 2 programs?
    By ModeZt in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2008, 21:23

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
  •  
Qt is a trademark of The Qt Company.