Results 1 to 3 of 3

Thread: To Share a variable among classes

  1. #1

    Default To Share a variable among classes

    Qt Code:
    1. //A.h
    2. class A
    3. {
    4. public:
    5. int quantity[8];
    6. int m;
    7.  
    8. }
    9.  
    10. //B.h
    11. class B
    12. {
    13. public:
    14. void getquantity ();
    15.  
    16.  
    17. }
    18.  
    19. //B.cpp
    20.  
    21. void B::getquantity()
    22. {
    23. //how can I get access to quantity or any variables from A? when I try A::m- I get errors
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 12th May 2011 at 12:09. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: To Share a variable among classes

    make m static,
    get a private instance of A in B
    use a singleton pattern
    etc...

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

    Default Re: To Share a variable among classes

    @JenniferF:
    This is a symptom of bad design, since it breaks OOP principals.
    It usually should not come to this with a correct design.
    Lykurgs suggestions are correct, but the fact they need to be used is already the problem.
    If you explain what it is you want to achieve, we probably can offer you better design approach.
    ==========================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.

Similar Threads

  1. Log in to a network share
    By Thomas_Lerman in forum Qt Programming
    Replies: 0
    Last Post: 28th April 2011, 16:47
  2. how to share variables
    By sksingh73 in forum Newbie
    Replies: 4
    Last Post: 2nd July 2010, 05:54
  3. Share a variable between forms
    By sepehr in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2009, 08:05
  4. cannot share the database connection!!!!
    By cbarmpar in forum Qt Programming
    Replies: 13
    Last Post: 23rd September 2008, 15:42

Tags for this Thread

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.