pointer to member reference

D

Daniel Aarno

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
 
D

DrNoose

Daniel said:
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
 
D

DrNoose

DrNoose said:
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!!!!!
 
M

mlimber

Daniel said:
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
 
R

Razzer

Daniel said:
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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top