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

This is a discussion on warning C4355: 'this' : used in base member initializer list within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm passing a reference to an object to another object which is hold by composition like this: Code: class ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

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

    Hi,

    I'm passing a reference to an object to another object which is hold by composition like this:

    Code:
    class A;
    
    class B : boost::noncopyable
    {
      A& mParent;
    public:
      B(A& parent)
         :mParent(parent)
      {}
    };
    
    class A : boost::noncopyable
    {
      B mB;
    public:
      A()                 // warning
        :mB(*this)
      {}
    };
    
    int main() {}
    and I'm getting the following warning, which was never apparent to me before (while using composition regulary):
    warning C4355: 'this' : used in base member initializer list
    What I'm doing wrong this monday?

    THank you in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    *this is not valid BEFORE the object has been created, so before you are INSIDE the constructor. The initializer list is performed "outside" the constructor, so *this is (technically) not valid.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thank you. What style is more common as a solution: Giving B a method

    void setParent(A& parent);

    to allow some form of 2-step-creation, or holding the handle to the parent as const pointer?

    EDIT:
    I see, passing this leads to the same warning as passing *this, so the first one is the only option, is it?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think a two-step solution is the right thing here - after all, you want to construct the mB from an existing A object - you can't do that until A has been constructed properly, which would be at the end of the constructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    "this" is perfectly valid in the constructor - it's just that the object it points to is not fully constructed, so using it is undefined behaviour.

    However, if all B does in its constructor is store the reference, passing *this in the initializer list is perfectly safe, and you can ignore the warning.

    Two-step construction is almost never the right solution, in my opinion.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Hi,

    Quote Originally Posted by CornedBee View Post
    Two-step construction is almost never the right solution, in my opinion.
    What is the reason for this?

    I found out why I never saw this warning: g++ doesn't produce it even with -Wall (It seems to be more causal regarding warnings in general)

    Some code review guys are a bit pedantic about warnings, maybe I'll just merge the two classes. They are strongly coupled anyway

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What is the reason for this?
    It makes the object considerably more complicated, both in usage and implementation. As a user, you have to worry about whether the object is fully constructed - it's very possible to forget the second step. With a single stage, you never have to worry about this. As an implementor, you also have to worry about the partially constructed stage, namely you have to insert some sort of check into every member function that the object is fully constructed - even if it's just an assert and the release behaviour is undefined.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    is there a way to suppress this warning?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    #pragma warning( disable : 4355 )
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    #pragma warning(push)
    #pragma warning(disable: 4355)
    #pragma warning(pop)

    This will disable a warning for a range of code only. It's up to you if you want to permanently disable it or not. It's also possible to ignore warnings in the project options under C/C++ -> Advanced.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21