A
Andrew Maclean
Please look at the sample code below first.
I understand the problem but I don't know how to get around it. I can see
that I am passing a pointer to an unconsructed object to another
constructor. How can I ensure that all construction is complete before using
the "this" pointer. Can someone give me some help on this?
Thanks in advance, Andrew.
Look at this example code:
// this class is an ultimate stream associated with a socket
template <class charT, class traits = std::char_traits<charT> >
class TCPGenericStream :
private TCPStreamBuffer<charT, traits>,
public std::basic_iostream<charT, traits>
{
public:
// this constructor takes 'ownership' of the socket wrapper if btakeowner
== true,
// so that the socket will be closed in the destructor of the
// TCPStreamBuffer object
explicit TCPGenericStream(TCPSocketWrapper &sock, bool takeowner = false)
: TCPStreamBuffer<charT, traits>(sock, takeowner),
std::basic_iostream<charT, traits>(this)
{
}
private:
// not for use
TCPGenericStream(const TCPGenericStream&);
TCPGenericStream& operator=(const TCPGenericStream&);
};
and
// this is even more specialized for use as a client
template <class charT, class traits = std::char_traits<charT> >
class TCPGenericClientStream :
private TCPSocketWrapper,
public TCPGenericStream<charT, traits>
{
public:
TCPGenericClientStream(const char *address, int port)
: TCPGenericStream<charT, traits>(*this, false)
{
TCPSocketWrapper::connect(address, port);
}
private:
// not for use
TCPGenericClientStream(const TCPGenericClientStream&);
TCPGenericClientStream& operator=(const TCPGenericClientStream&);
};
I understand the problem but I don't know how to get around it. I can see
that I am passing a pointer to an unconsructed object to another
constructor. How can I ensure that all construction is complete before using
the "this" pointer. Can someone give me some help on this?
Thanks in advance, Andrew.
Look at this example code:
// this class is an ultimate stream associated with a socket
template <class charT, class traits = std::char_traits<charT> >
class TCPGenericStream :
private TCPStreamBuffer<charT, traits>,
public std::basic_iostream<charT, traits>
{
public:
// this constructor takes 'ownership' of the socket wrapper if btakeowner
== true,
// so that the socket will be closed in the destructor of the
// TCPStreamBuffer object
explicit TCPGenericStream(TCPSocketWrapper &sock, bool takeowner = false)
: TCPStreamBuffer<charT, traits>(sock, takeowner),
std::basic_iostream<charT, traits>(this)
{
}
private:
// not for use
TCPGenericStream(const TCPGenericStream&);
TCPGenericStream& operator=(const TCPGenericStream&);
};
and
// this is even more specialized for use as a client
template <class charT, class traits = std::char_traits<charT> >
class TCPGenericClientStream :
private TCPSocketWrapper,
public TCPGenericStream<charT, traits>
{
public:
TCPGenericClientStream(const char *address, int port)
: TCPGenericStream<charT, traits>(*this, false)
{
TCPSocketWrapper::connect(address, port);
}
private:
// not for use
TCPGenericClientStream(const TCPGenericClientStream&);
TCPGenericClientStream& operator=(const TCPGenericClientStream&);
};