pointer to member reference

Discussion in 'C++' started by Daniel Aarno, Oct 17, 2005.

  1. Daniel Aarno

    Daniel Aarno Guest

    Consider the following code:

    typedef int A;

    class B {
    public:
    B(A& var) : m_var(var) {}
    virtual ~B() {}

    protected:
    A &m_var;
    };

    class C : public B {
    public:
    C(A* a) : B(*a) {}
    ~C();
    };

    int main()
    {
    A* a = new A();
    C c(a);
    }

    I now want to implement the destructor of C to deallocate a (which is
    referenced through A::m_var).

    I tried this code:

    C::~C()
    {
    delete &B::m_var;
    }

    which gave me the following error

    tst.cc: In destructor `virtual C::~C()':
    tst.cc:9: error: `A&B::m_var' is protected
    tst.cc:26: error: within this context
    tst.cc:26: error: cannot create pointer to reference member `B::m_var'

    Hmmm, that was strange, I have "always" gotten pointer to member references...
    so I tried the follwing:

    C::~C()
    {
    A& tmp = B::m_var;
    delete &tmp;
    }

    which worked fine. Just to make sure I also tried

    C::~C()
    {
    A* tmp = &B::m_var;
    delete tmp;
    }

    which (as I now expected) gave me a similar error to the one above.
    Finnaly I settled for the implementation shown below which also worked fine,
    i.e. I removed the scoping to B.

    C::~C()
    {
    delete &m_var;
    }

    Can anyone please elaborate exactly what is the correct way to go here and why
    one works and not the other, they all seem to be the same thing to me but
    probably I'm missing out on some details here.

    /Daniel Aarno
     
    Daniel Aarno, Oct 17, 2005
    #1
    1. Advertising

  2. Daniel Aarno

    DrNoose Guest

    Daniel Aarno wrote:
    > Consider the following code:
    >
    > typedef int A;
    >
    > class B {
    > public:
    > B(A& var) : m_var(var) {}
    > virtual ~B() {}
    >
    > protected:
    > A &m_var;
    > };
    >
    > class C : public B {
    > public:
    > C(A* a) : B(*a) {}
    > ~C();
    > };
    >
    > int main()
    > {
    > A* a = new A();
    > C c(a);
    > }
    >
    > I now want to implement the destructor of C to deallocate a (which is
    > referenced through A::m_var).
    >
    > I tried this code:
    >
    > C::~C()
    > {
    > delete &B::m_var;
    > }
    >
    > which gave me the following error
    >
    > tst.cc: In destructor `virtual C::~C()':
    > tst.cc:9: error: `A&B::m_var' is protected
    > tst.cc:26: error: within this context
    > tst.cc:26: error: cannot create pointer to reference member `B::m_var'
    >
    > Hmmm, that was strange, I have "always" gotten pointer to member
    > references... so I tried the follwing:
    >
    > C::~C()
    > {
    > A& tmp = B::m_var;
    > delete &tmp;
    > }
    >
    > which worked fine. Just to make sure I also tried
    >
    > C::~C()
    > {
    > A* tmp = &B::m_var;
    > delete tmp;
    > }
    >
    > which (as I now expected) gave me a similar error to the one above.
    > Finnaly I settled for the implementation shown below which also worked
    > fine, i.e. I removed the scoping to B.
    >
    > C::~C()
    > {
    > delete &m_var;
    > }
    >
    > Can anyone please elaborate exactly what is the correct way to go here
    > and why one works and not the other, they all seem to be the same thing
    > to me but probably I'm missing out on some details here.
    >
    > /Daniel Aarno

    Daniel,

    Hi!

    I appreciate your input, but you totally lost me! I'm just starting out
    in C++ and am very ignorant! LOL. Until I learn more, I need everything
    as simple as possible!

    Some of the code that I have comes from the teacher and I have to try
    and stay within what he has. I can get the output he shows in his
    example which was the "I say Hi....", but I want to make sure that when
    I test it with other data, I don't get garbage.

    Thanks!

    Kathy
     
    DrNoose, Oct 17, 2005
    #2
    1. Advertising

  3. Daniel Aarno

    DrNoose Guest

    DrNoose wrote:
    > Daniel Aarno wrote:
    >
    >> Consider the following code:
    >>
    >> typedef int A;
    >>
    >> class B {
    >> public:
    >> B(A& var) : m_var(var) {}
    >> virtual ~B() {}
    >>
    >> protected:
    >> A &m_var;
    >> };
    >>
    >> class C : public B {
    >> public:
    >> C(A* a) : B(*a) {}
    >> ~C();
    >> };
    >>
    >> int main()
    >> {
    >> A* a = new A();
    >> C c(a);
    >> }
    >>
    >> I now want to implement the destructor of C to deallocate a (which is
    >> referenced through A::m_var).
    >>
    >> I tried this code:
    >>
    >> C::~C()
    >> {
    >> delete &B::m_var;
    >> }
    >>
    >> which gave me the following error
    >>
    >> tst.cc: In destructor `virtual C::~C()':
    >> tst.cc:9: error: `A&B::m_var' is protected
    >> tst.cc:26: error: within this context
    >> tst.cc:26: error: cannot create pointer to reference member `B::m_var'
    >>
    >> Hmmm, that was strange, I have "always" gotten pointer to member
    >> references... so I tried the follwing:
    >>
    >> C::~C()
    >> {
    >> A& tmp = B::m_var;
    >> delete &tmp;
    >> }
    >>
    >> which worked fine. Just to make sure I also tried
    >>
    >> C::~C()
    >> {
    >> A* tmp = &B::m_var;
    >> delete tmp;
    >> }
    >>
    >> which (as I now expected) gave me a similar error to the one above.
    >> Finnaly I settled for the implementation shown below which also worked
    >> fine, i.e. I removed the scoping to B.
    >>
    >> C::~C()
    >> {
    >> delete &m_var;
    >> }
    >>
    >> Can anyone please elaborate exactly what is the correct way to go here
    >> and why one works and not the other, they all seem to be the same
    >> thing to me but probably I'm missing out on some details here.
    >>
    >> /Daniel Aarno

    >
    > Daniel,
    >
    > Hi!
    >
    > I appreciate your input, but you totally lost me! I'm just starting out
    > in C++ and am very ignorant! LOL. Until I learn more, I need everything
    > as simple as possible!
    >
    > Some of the code that I have comes from the teacher and I have to try
    > and stay within what he has. I can get the output he shows in his
    > example which was the "I say Hi....", but I want to make sure that when
    > I test it with other data, I don't get garbage.
    >
    > Thanks!
    >
    > Kathy

    Oppppps!!!!

    Sorry! I responded to the wrong message!!!!!
     
    DrNoose, Oct 17, 2005
    #3
  4. Daniel Aarno

    mlimber Guest

    Daniel Aarno wrote:
    > Consider the following code:
    >
    > typedef int A;
    >
    > class B {
    > public:
    > B(A& var) : m_var(var) {}
    > virtual ~B() {}
    >
    > protected:
    > A &m_var;
    > };
    >
    > class C : public B {
    > public:
    > C(A* a) : B(*a) {}
    > ~C();
    > };
    >
    > int main()
    > {
    > A* a = new A();
    > C c(a);
    > }
    >
    > I now want to implement the destructor of C to deallocate a (which is
    > referenced through A::m_var).
    >
    > I tried this code:
    >
    > C::~C()
    > {
    > delete &B::m_var;
    > }
    >
    > which gave me the following error
    >
    > tst.cc: In destructor `virtual C::~C()':
    > tst.cc:9: error: `A&B::m_var' is protected
    > tst.cc:26: error: within this context
    > tst.cc:26: error: cannot create pointer to reference member `B::m_var'
    >
    > Hmmm, that was strange, I have "always" gotten pointer to member references...
    > so I tried the follwing:
    >
    > C::~C()
    > {
    > A& tmp = B::m_var;
    > delete &tmp;
    > }
    >
    > which worked fine. Just to make sure I also tried
    >
    > C::~C()
    > {
    > A* tmp = &B::m_var;
    > delete tmp;
    > }
    >
    > which (as I now expected) gave me a similar error to the one above.
    > Finnaly I settled for the implementation shown below which also worked fine,
    > i.e. I removed the scoping to B.
    >
    > C::~C()
    > {
    > delete &m_var;
    > }
    >
    > Can anyone please elaborate exactly what is the correct way to go here and why
    > one works and not the other, they all seem to be the same thing to me but
    > probably I'm missing out on some details here.
    >
    > /Daniel Aarno


    I'd suggest that converting a heap-allocated pointer to a reference and
    then deallocating the memory pointed to by a reference is a very
    confusing and poor practice. Prefer instead to use a smart pointer such
    as std::auto_ptr or boost::scoped_ptr or boost::shared_ptr. In
    particular, std::auto_ptr communicates that ownership is taken over by
    the class. Consider:

    typedef int A;

    class B {
    public:
    B( std::auto_ptr<A>& var) : m_var(var) {}
    virtual ~B() {}

    protected:
    std::auto_ptr<A> m_var;
    };

    class C : public B {
    public:
    C(std::auto_ptr<A>& a) : B(a) {}
    };

    int main()
    {
    std::auto_ptr<A> a( new A );
    C c(a);
    // ...
    return 0;
    }

    And now no one has to remember to clean up after the new A.

    Cheers! --M
     
    mlimber, Oct 17, 2005
    #4
  5. Daniel Aarno

    Razzer Guest

    Daniel Aarno wrote:
    > C::~C()
    > {
    > delete &B::m_var;
    > }
    > Can anyone please elaborate exactly what is the correct way to go here and why
    > one works and not the other, they all seem to be the same thing to me but
    > probably I'm missing out on some details here.


    This is not referring to the member variable your object C has. This is
    a pointer to member of class B. If you are trying to do something like
    that, you probably should do something like:

    C::~C()
    {
    delete &(this->B::m_var);
    }

    Or, shorter:

    C::~C()
    {
    delete &(B::m_var);
    }
     
    Razzer, Oct 17, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,094
    Fraser Ross
    Aug 14, 2004
  2. A
    Replies:
    7
    Views:
    657
  3. Stephen Howe
    Replies:
    2
    Views:
    306
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    311
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    178
    somenath
    Aug 29, 2013
Loading...

Share This Page