Thread: How can a class or struct contain a copy of its own type?

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

    How can a class or struct contain a copy of its own type?

    Hi there,

    I was perusing through some code and I found this:

    Code:
    struct CD3DX12_RESOURCE_BARRIER : public D3D12_RESOURCE_BARRIER
    {
        CD3DX12_RESOURCE_BARRIER()
        {}
        explicit CD3DX12_RESOURCE_BARRIER(const D3D12_RESOURCE_BARRIER &o) :
            D3D12_RESOURCE_BARRIER(o)
        {}
    
        static inline CD3DX12_RESOURCE_BARRIER Transition(
            _In_ ID3D12Resource* pResource,
            D3D12_RESOURCE_STATES stateBefore,
            D3D12_RESOURCE_STATES stateAfter,
            UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
            D3D12_RESOURCE_BARRIER_FLAGS flags = D3D12_RESOURCE_BARRIER_FLAG_NONE)
        {
            CD3DX12_RESOURCE_BARRIER result;
            ZeroMemory(&result, sizeof(result));
            D3D12_RESOURCE_BARRIER &barrier = result;
            result.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
            result.Flags = flags;
            barrier.Transition.pResource = pResource;
            barrier.Transition.StateBefore = stateBefore;
            barrier.Transition.StateAfter = stateAfter;
            barrier.Transition.Subresource = subresource;
            return result;
        }
    
    .... code continues
    };
    So there's a struct defined as inheriting from another one and then some constructors. Then comes the static function. Inside the static function there is an object of this exact class type:

    Code:
    CD3DX12_RESOURCE_BARRIER result;
    So I wonder how does this not cause some bizarre sort of declaration recursion as surely that means there will be another copy of the class, with another function of the same type with another object of the class type and then another and another...... Unlike a recursive function though there is no condition guaranteed to eventually cause one iteration of this to be firmly resolved. Rather like a room with two mirrors facing each other on opposite walls.

    How does the compiler sort of know when to stop?

    It's got me quite confused!, thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    The object is defined inside a function, not inside the struct, so there's nothing recursive about it.
    It would be recursive (and impossible) if it was like this:
    Code:
    struct A {
        A a;    // this is not allowed
        int x;
        
        void func() {
            A a2; // this is okay
            ...
        }
    };
    It is, however, possible to have a pointer to the object inside itself.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Thanks very much for clearing that up John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dealing with '... is not a class, struct, or union type' error
    By Omar Sharaki in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2018, 09:20 AM
  2. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 02:11 PM
  3. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  4. vector - left of .push_back must have class/struct/union type
    By patricio2626 in forum C++ Programming
    Replies: 5
    Last Post: 11-18-2006, 04:37 PM
  5. copy struct to class
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2003, 09:27 PM

Tags for this Thread