Eae
Eae

Reputation: 4321

C++, ampersand after parameter name in class constructor

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.

Upvotes: 2

Views: 2656

Answers (1)

Dietmar Kühl
Dietmar Kühl

Reputation: 154005

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.

Upvotes: 6

Related Questions