pointer reference

This is a discussion on pointer reference within the C++ Programming forums, part of the General Programming Boards category; Hello I want to pass 'this' as a reference to another class's constructor: Code: class A { public: A() : ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    pointer reference

    Hello

    I want to pass 'this' as a reference to another class's constructor:

    Code:
    class A {
    public:
    A() : m_b(*this) { }
    B m_b;
    };
    
    class B {
    public:
    B(A &ref) : m_ref(ref) { }
    A &m_ref;
    };
    Is this the right way to do it?
    I think the red line is critical..

    Thanks for help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Yes, although many compilers will give you a warning since the A object won't be fully constructed at the time you pass it to B's constructor. If you only store the reference and don't actually use the A object it should be fine in practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 03:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21