Thread: Using 'this' in Initializer List?

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Using 'this' in Initializer List?

    This could have to do with windows programming, as it could have to do with the specifics of how I'm using the this pointer, however, I'm gonna ask here. KK. K. Here's how it's all laid out.

    Code:
    window procedure - recieves 'this' of OGL manager in lpCreateParams
    
    opengl class - has-a window class which takes lpCreateParams as a constructor arg
    window class - creates window using window procedure and the create params it recieves from opengl class
    
    
    class w {w(LPVOID createparams) { createwindow(use wndproc, createparams); }
    class ogl { friend LRESULT wndproc(blah, blah); window _w; ogl() : _w(this) { } };
    LRESULT wndproc(blah, blah) { uses some private routines of ogl; }
    I get a warning from passing the this of my ogl class to my window class:

    Code:
    warning C4355: 'this' : used in base member initializer list
    Wot? How dangerous, bad, and crazy is this potentially? Workarounds?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Wow that's some ugly code right there. You know, putting all the code on one line does not make it faster, better or anything. Indent it.

    The problem is that you are passing a pointer to an object that is not even constructed to another object.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How dangerous, bad, and crazy is this potentially? Workarounds?
    It would be fine as long as you were just storing the value for later use and not actually calling methods from the pointer. If you are actually trying to use member functions or variables of the object before its constructor is called, then you could have issues.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I figurred out a workaround. Thanks n.e.w.a.y.s.

    >> Wow that's some ugly code right there. You know, putting all the code on one line does not make it faster, better or anything. Indent it.

    It's psuedo-code :<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM