* vector<MyTemplate<MyClass>> not possible?
@ 2002-09-20 0:29 Evert, F.K. van
2002-09-20 1:12 ` Sebastian Huber
2002-09-20 2:13 ` Claudio Bley
0 siblings, 2 replies; 3+ messages in thread
From: Evert, F.K. van @ 2002-09-20 0:29 UTC (permalink / raw)
To: 'gcc-help@gcc.gnu.org'
When I put instances of a template-derived class in a STL vector, g++ gives
me the following error message (Cygwin/g++3.2 as well as Suse8.0/g++2.95.3):
/usr/include/c++/3.2/bits/stl_construct.h:78: passing `const SmartPtr<CX>'
as
`this' argument of `SmartPtr<T>::operator T*() [with T = CX]' discards
qualifiers
I have seen similar error messages reported about auto_ptr derived classes
in STL containers, but my template class seems to have all the required
methods.
The code below compiles and runs fine with VC++ and BC++ (Kylix3/Suse8.0). I
would greatly appreciate any help that you can offer.
Regards,
Frits
===== main file ===========================================================
#include <stdio.h>
#include <vector>
template <class T> class SmartPtr
{
private:
T* m_pI;
public:
SmartPtr(T* p = NULL)
{
m_pI = p;
if (m_pI != NULL)
m_pI->AddRef();
}
~SmartPtr()
{
if (m_pI != NULL)
{
T* old = m_pI;
m_pI = NULL;
old->Release();
}
}
SmartPtr(const SmartPtr& r) throw()
{
m_pI = r.m_pI;
if (m_pI != NULL)
m_pI->AddRef();
}
SmartPtr& operator=(const SmartPtr& lp)
{
if (m_pI != lp.m_pI)
{
T* pOld = m_pI ;
m_pI = lp.m_pI ;
if (m_pI != NULL)
{
m_pI->AddRef() ;
}
if (pOld != NULL)
{
pOld->Release() ;
}
}
return *this;
}
T* operator->() { return m_pI; }
T** operator&() { return &m_pI ;}
T& operator*() { return *m_pI; }
operator T*() { return m_pI ;}
};
class CX {
public:
CX() { printf("CX::CX()\n"); cnt = 0; }
~CX() { printf("CX::~CX()\n"); }
void AddRef() { ++cnt; }
void Release() { --cnt; if (cnt==0) delete this; }
private:
long cnt;
};
typedef SmartPtr<CX> XPtr;
int main()
{
CX* px = new CX();
XPtr sp(px);
std::vector<XPtr> v;
v.push_back(sp); // THIS IS THE LINE THAT OFFENDS G++
return 0;
}
F.K. van Evert
Plant Research International
Phone (+31) 317-475957, Fax -423110
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: vector<MyTemplate<MyClass>> not possible?
2002-09-20 0:29 vector<MyTemplate<MyClass>> not possible? Evert, F.K. van
@ 2002-09-20 1:12 ` Sebastian Huber
2002-09-20 2:13 ` Claudio Bley
1 sibling, 0 replies; 3+ messages in thread
From: Sebastian Huber @ 2002-09-20 1:12 UTC (permalink / raw)
To: gcc-help
Hallo,
you try to use a non-const method on a constant object ...
On Friday 20 September 2002 00:29, Evert, F.K. van wrote:
> When I put instances of a template-derived class in a STL vector, g++ gives
> me the following error message (Cygwin/g++3.2 as well as
> Suse8.0/g++2.95.3):
>
> /usr/include/c++/3.2/bits/stl_construct.h:78: passing `const SmartPtr<CX>'
> as
> `this' argument of `SmartPtr<T>::operator T*() [with T = CX]' discards
> qualifiers
>
> I have seen similar error messages reported about auto_ptr derived classes
> in STL containers, but my template class seems to have all the required
> methods.
>
> The code below compiles and runs fine with VC++ and BC++ (Kylix3/Suse8.0).
> I would greatly appreciate any help that you can offer.
>
> Regards,
>
> Frits
>
> ===== main file ===========================================================
> #include <stdio.h>
> #include <vector>
>
> template <class T> class SmartPtr
> {
> private:
> T* m_pI;
> public:
>
> SmartPtr(T* p = NULL)
> {
> m_pI = p;
> if (m_pI != NULL)
> m_pI->AddRef();
> }
>
> ~SmartPtr()
> {
> if (m_pI != NULL)
> {
> T* old = m_pI;
> m_pI = NULL;
> old->Release();
> }
> }
>
> SmartPtr(const SmartPtr& r) throw()
> {
> m_pI = r.m_pI;
> if (m_pI != NULL)
> m_pI->AddRef();
> }
>
> SmartPtr& operator=(const SmartPtr& lp)
> {
> if (m_pI != lp.m_pI)
> {
> T* pOld = m_pI ;
> m_pI = lp.m_pI ;
> if (m_pI != NULL)
> {
> m_pI->AddRef() ;
> }
> if (pOld != NULL)
> {
> pOld->Release() ;
> }
> }
> return *this;
> }
>
> T* operator->() { return m_pI; }
> T** operator&() { return &m_pI ;}
> T& operator*() { return *m_pI; }
> operator T*() { return m_pI ;}
Try: 'operator T*() const { return m_pl; };'
This works because you return a copy of the pointer.
>
> };
>
> class CX {
> public:
> CX() { printf("CX::CX()\n"); cnt = 0; }
> ~CX() { printf("CX::~CX()\n"); }
> void AddRef() { ++cnt; }
> void Release() { --cnt; if (cnt==0) delete this; }
> private:
> long cnt;
> };
>
> typedef SmartPtr<CX> XPtr;
>
> int main()
> {
> CX* px = new CX();
> XPtr sp(px);
> std::vector<XPtr> v;
> v.push_back(sp); // THIS IS THE LINE THAT OFFENDS G++
> return 0;
> }
>
>
> F.K. van Evert
> Plant Research International
> Phone (+31) 317-475957, Fax -423110
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: vector<MyTemplate<MyClass>> not possible?
2002-09-20 0:29 vector<MyTemplate<MyClass>> not possible? Evert, F.K. van
2002-09-20 1:12 ` Sebastian Huber
@ 2002-09-20 2:13 ` Claudio Bley
1 sibling, 0 replies; 3+ messages in thread
From: Claudio Bley @ 2002-09-20 2:13 UTC (permalink / raw)
To: Evert, F.K. van; +Cc: 'gcc-help@gcc.gnu.org'
>>>>> "FK" == Evert, F K van <F.K.vanEvert@plant.wag-ur.nl> writes:
FK> When I put instances of a template-derived class in a STL
FK> vector, g++ gives me the following error message
FK> (Cygwin/g++3.2 as well as Suse8.0/g++2.95.3):
FK> /usr/include/c++/3.2/bits/stl_construct.h:78: passing `const
FK> SmartPtr<CX>' as `this' argument of `SmartPtr<T>::operator
FK> T*() [with T = CX]' discards qualifiers
FK> operator T*() { return m_pI ;}
should be: operator T*() const { return m_pI; }
Generally, it's a good idea to declare all member functions `const'
which don't change the state of their class instance.
--
Claudio Bley _
ASCII ribbon campaign ( )
ICQ# 83197241 - against HTML email X
http://www.cs.uni-magdeburg.de/~bley/ & vCards / \
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2002-09-20 9:13 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-20 0:29 vector<MyTemplate<MyClass>> not possible? Evert, F.K. van
2002-09-20 1:12 ` Sebastian Huber
2002-09-20 2:13 ` Claudio Bley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).