Results 1 to 3 of 3

Thread: C++ pointer by reference problem.

  1. #1
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default C++ pointer by reference problem.

    Hello.

    I have problems with pointers by reference topic. This is the isue:

    I have in a qt program:

    - One class "A" with a class "B" and "C" instanced inside.
    - A class D. This constains a set of Strings. And it is declared as a data member in "A" ,"B" and "C".


    The problem is that I have to modify the "D" values (I have an instance of D class in A ,B and C) in any of the three classes and I need to manage a same value.

    So, I think:


    Qt Code:
    1. Class A.
    2. {
    3. B * m_b;
    4. C * m_c;
    5. D * m_d;
    6.  
    7.  
    8. A()
    9. {
    10. m_b= new B(&m_d);
    11. m_c= new C(&m_d);
    12. }
    13. }
    14.  
    15. Class B.
    16. {
    17. D* m_d;
    18.  
    19. B(D* &d)
    20. {
    21. m_d=d
    22. }
    23. }
    24.  
    25.  
    26.  
    27. Class C.
    28. {
    29. D* m_d;
    30.  
    31. C(D* &d)
    32. {
    33. m_d=d
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 




    My problem is that before y create m_d in B, I cant see the address in m_d of C.

    Plese...I need your help

    Thanks a lot!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ pointer by reference problem.

    If you only need to modify the values of the D instance, you can simply pass pointers to the D instance.
    You only need a reference to the pointer if you need to change the pointer itself.

    Just create the D instance before you create the B and C instance.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: C++ pointer by reference problem.

    thanks to all of you!!

    My problem were....I passed the pointer by reference before I declared it.. lol..XD!

    The only unusual issue is that the compiler give not me an error.

    Thaks!!

Similar Threads

  1. Replies: 3
    Last Post: 23rd May 2015, 13:57
  2. pointer reference confusion
    By sincnarf in forum General Programming
    Replies: 1
    Last Post: 14th October 2007, 13:42
  3. problem with pointer-reference
    By mickey in forum General Programming
    Replies: 10
    Last Post: 24th February 2007, 21:59
  4. reference problem
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2006, 12:13
  5. pointer/reference
    By mickey in forum General Programming
    Replies: 7
    Last Post: 16th July 2006, 14:54

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.