473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

warning C4355: 'this' : used in base member initializer list

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&);
};
Nov 16 '05 #1
1 14380
Andrew Maclean wrote:
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?


Keep in mind that C4355 is only a warning - the code has well defined
behavior as long as the base class constructor doesn't attempt to
dereference that 'this' value in the constructor body. If you're satisfied
that that's the case, then the solution is #pragma warning(disable:4355).

The compiler issues a warning for this usage because it's easy to invoke
undefined behavior when passing this to a base class constructor - as long
as you know what you're doing though, there's nothing technically wrong with
it.

-cd
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dave | last post by:
class base { public: base(const base &other) { // Init. here... } // Stuff }; class derived: public base { public:
1
by: Derek | last post by:
I get the following warning warning C4355: 'this' : used in base member initializer list when I compile the following program with VC6 and VC7 (the latest GCC and Comeau doesn't think anything...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
4
by: l.s.rockfan | last post by:
Hello, how do i have to call an inherited, templated class constructor from the initializer list of the inheriting, non-templated class constructor? example code: template<typename T>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
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...
0
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...
0
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...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.