Re: What's wrong with this class
John Doe schrieb:
But Clear changes the class, at least logically, so making Clear() const
is wrong. Why do you need the function anyway?
You don't need to define an empty destructor.
getName should be const.
Clear deletes the objects in your list, but you don't clear the list
itself. You have dangling pointers.
[...]
Here you call delete on all your pointers, but you don't clear _netlist.
After that, you use these dangling pointers, which is undefined
behaviour and most likely will crash your program.
Why do you clear the list just before you use it?
Just as the other posters suggested, the easiest is not to use pointers
at all and change the member variable to:
std::list<Netwo rknetlist; // std::vector would do it as well.
--
Thomas
John Doe schrieb:
Victor Bazarov wrote:
>
So here is the update code (without the try for new I will do it later)
but now I get an error :
error C2662: 'NetworkList::C lear' : cannot convert 'this' pointer from
'const NetworkList' to 'NetworkList &'
>
so I have added const to Clear()...
>John Doe wrote:
So here is the update code (without the try for new I will do it later)
but now I get an error :
error C2662: 'NetworkList::C lear' : cannot convert 'this' pointer from
'const NetworkList' to 'NetworkList &'
>
so I have added const to Clear()...
is wrong. Why do you need the function anyway?
//=============== =============== ==============//
// NetworkManager. h
//=============== =============== ==============//
struct DeletePointer {
template<typena me T>
void operator()(cons t T* ptr) const
{
delete ptr;
}
};
>
class Network
{
public:
Network() {};
Network(CString const& netName, GUID netguid):
_netname(netNam e), _netguid(netgui d) {}
>
~Network() {}
// NetworkManager. h
//=============== =============== ==============//
struct DeletePointer {
template<typena me T>
void operator()(cons t T* ptr) const
{
delete ptr;
}
};
>
class Network
{
public:
Network() {};
Network(CString const& netName, GUID netguid):
_netname(netNam e), _netguid(netgui d) {}
>
~Network() {}
CString const& getName() { return _netname; }
GUID getGuid() const { return _netguid; }
>
private:
CString _netname;
GUID _netguid;
};
>
>
class NetworkList
{
typedef std::list<Netwo rk*>::iterator NetworkListIt;
>
public:
>
NetworkList()
{
}
>
~NetworkList()
{
Clear();
}
>
void Clear()
{
for_each( _netlist.begin( ), _netlist.end(), DeletePointer ());
}
>
private:
CString _netname;
GUID _netguid;
};
>
>
class NetworkList
{
typedef std::list<Netwo rk*>::iterator NetworkListIt;
>
public:
>
NetworkList()
{
}
>
~NetworkList()
{
Clear();
}
>
void Clear()
{
for_each( _netlist.begin( ), _netlist.end(), DeletePointer ());
}
itself. You have dangling pointers.
[...]
const Network* getNetwork(CLSI D guid) const
{
if (!_netlist.empt y())
Clear();
{
if (!_netlist.empt y())
Clear();
After that, you use these dangling pointers, which is undefined
behaviour and most likely will crash your program.
Why do you clear the list just before you use it?
std::list<Netwo rk*>::const_ite rator it;
//NetworkListIt it;
for (it = _netlist.begin( ); it != _netlist.end(); ++it)
if ((*it)->getGuid() == guid)
return (*it);
>
return NULL;
}
>
private:
std::list<Netwo rk*_netlist;
};
//NetworkListIt it;
for (it = _netlist.begin( ); it != _netlist.end(); ++it)
if ((*it)->getGuid() == guid)
return (*it);
>
return NULL;
}
>
private:
std::list<Netwo rk*_netlist;
};
at all and change the member variable to:
std::list<Netwo rknetlist; // std::vector would do it as well.
--
Thomas
Comment