Thread: Class Member Function and Scope Question

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Question Class Member Function and Scope Question

    Hi there,

    I was wondering about something I noticed lately. I've been re-doing the dialog box program I wrote some time back to use a virtual class base-inherit style interface system. It means any code in the main program can call functions inside a DLL where all the DirectX stuff (or other API if you were using one) is stored. That way the rendering specific stuff says away from the main program.

    It's an idea I got from an old book I've kind of adopted as a personal code bible. Was trickier to implement than I thought. Obviously delegating things like this through an interface can cause issues with acquiring valid pointers to objects and thus functions etc... I sorted it out in the end without having to make every function in the rendering class static (which would have looked bad) but I noticed this:

    Code:
    LONG_PTR ptr = GetWindowLongPtr(GetParent(hDlg), GWLP_USERDATA);
    static D3DRenderer* pD3DRenderer = reinterpret_cast<D3DRenderer*>(ptr);
    
    ....sometime later
    
    pD3DRenderer->LogResolution();
    And a short part of the LogResolution() function:

    Code:
    UINT count = 0;
    UINT flags = 0;
    UINT refreshRate = 0;
    
    IDXGIAdapter* adapter = nullptr;
    IDXGIOutput* output = nullptr;
    mResolutionPair.clear();
    Note the last variable at the end. It's a vector, not that that's important really I guess this would apply to any variable. My question is...

    How does this function know that the class data member mResolutionPair is tied to a specific object? It's not a static data member and nowhere in the function is any reference made to the actual object it belongs to. Yet it works.

    Does C++ by default use the object that called the function as its reference to where variables and functions are stored? So if I called the function:

    Code:
    pD3DRenderer->LogResolution();
    Does that mean that LogResolution() in this case uses the data stored in pD3DRenderer as its default location for stuff?

    Kinda had me a bit puzzled.

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Does that mean that LogResolution() in this case uses the data stored in pD3DRenderer as its default location for stuff?
    Yes.


    If you're familiar with the "this" pointer, then understand that "this" inside LogResolution() is pointing to the same object as pD3DRenderer. It always works that way.
    If you're not familiar with the "this" pointer then reading about that might help reinforce your understanding here.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Excellent! Thanks very much Daved. I'm familiar with the this keyword from believe it or not scripting language for Operation Flashpoint way back in the very early 2000's. I'll re-read some info on it again to update my knowledge in C++. Thanks very much indeed Daved!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2019, 04:10 AM
  2. Member function from class as a friend in another class
    By thames in forum C++ Programming
    Replies: 18
    Last Post: 01-02-2013, 05:32 AM
  3. Replies: 3
    Last Post: 07-25-2012, 05:32 PM
  4. Class Scope & Locking question
    By Toonzaka in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2011, 11:01 AM
  5. Function in a private member of a class?
    By RealityFusion in forum C++ Programming
    Replies: 9
    Last Post: 09-02-2005, 05:42 PM

Tags for this Thread