2

systemclass.h

class SystemClass
{
public:
    SystemClass();
    SystemClass(const SystemClass&);
    ~SystemClass();

    bool Initialize();
    void Shutdown();
    void Run();

    LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);

private:
    bool Frame();
    void InitializeWindows(int&, int&);
    void ShutdownWindows();

private:
    LPCWSTR m_applicationName;
    HINSTANCE m_hinstance;
    HWND m_hwnd;

    InputClass* m_Input;
    GraphicsClass* m_Graphics;
};

systemclass.cpp

SystemClass::SystemClass()
{
    m_Input = 0;
    m_Graphics = 0;
}


SystemClass::SystemClass(const SystemClass& other)
{
}


SystemClass::~SystemClass()
{
}

My question is in regards the the definition in systemclass.h:

SystemClass(const SystemClass&);

I don't recall ever seeing an "&" after a class name like this. What is the meaning of:

const SystemClass&

The code is coming from the C++ DirectX11 Tutorial 4 from rastertek.com.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Eae
  • 4,191
  • 15
  • 55
  • 92

1 Answers1

6

The & indicates that the argument is passed by reference rather than by value. The particular member you picked out happens to be the copy constructor, i.e., the constructor which is called when copying a value. If can't take its argument by value as the value would be produced by copying it...

The type const SystemClass& (or, putting the const into the right location SystemClass const&) is a reference to a const object of type SystemClass.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • 1
    I've always thought of the `&` mark on a parameter as indicating that the argument _aliases_ the parameter. Certainly one _way_ of implementing aliasing of argument and parameter is to pass by reference. Do you know if the C++ standard mandates passing by reference (an implementation concept) or if it just means aliasing (with its implementation being most likely a pass by reference). – Ray Toal Jan 10 '14 at 23:31
  • 2
    @RayToal I'm not sure I understand the distinction you're making. Passing by reference is a conceptual idea, with the usual under-the-hood implementation being "pass a pointer to the object and silently dereference the pointer when needed". – John Kugelman Jan 10 '14 at 23:37