Thread: pointer reference

  1. #1
    Registered User
    Join Date
    May 2006

    pointer reference


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

    class A {
    A() : m_b(*this) { }
    B m_b;
    class B {
    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
    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, 04:08 AM