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 4 1962
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 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!!!!!
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
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);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jimjim |
last post by:
Hello,
My question concerns as to how a pointer is passed by reference as a
function argument. The following is from code taken from the MICO
implementation of the CORBA specification.
in...
|
by: Steven T. Hatton |
last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe
it passed the moderator's veto - and I did not expect or desire anything
different. But the question remains:
ISO/IEC...
|
by: Itay_k |
last post by:
Hello,
I want a member in my class that will save pointer to pointer to
System::Drawing::Image class.
When I write on my class code:
System::Drawing::Image **bmp;
I get this error message:...
|
by: man |
last post by:
can any one please tell me what is the diff between pointer and
reference.....and which one is better to use ....and why???????
|
by: Steve Rencontre |
last post by:
I can't for the life of me see how to do pointer-to-member when the
member is actually part of an embedded structure.
That is, if I have:
struct S1 { int a; };
struct S2 { S1 s; int b; };
...
|
by: toton |
last post by:
HI,
One more small doubt from today's mail.
I have certain function which returns a pointer (sometimes a const
pointer from a const member function).
And certain member function needs reference...
|
by: Ney André de Mello Zunino |
last post by:
Hello.
I have written a simple reference-counting smart pointer class template
called RefCountPtr<T>. It works in conjunction with another class,
ReferenceCountable, which is responsible for the...
|
by: djcredo |
last post by:
Hey all,
I want to return a pointer to a struct. Here is what I'lm trying to do:
struct Position{
int x;
int y;
};
Position* GraphicTag::getRenderCentre(){
|
by: shuisheng |
last post by:
Dear All,
The problem of choosing pointer or reference is always confusing me.
Would you please give me some suggestion on it. I appreciate your kind
help.
For example, I'd like to convert a...
|
by: Howard |
last post by:
Hi,
I've got a program whose main object contains an array of "cells", each
of which contains an array of "sub-cells", each of which contains an array
of "sub-sub-cells". The cells, subcells...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |