Thread: Question about strange constructors in class

  1. #1
    Registered User
    Join Date
    Jan 2010

    Question Question about strange constructors in class

    Hi there!

    Have a look at the following code in a class:

    class D3DApp
    D3DApp(HINSTANCE hInstance);
    D3DApp(const D3DApp& rhs) = delete;
    D3DApp& operator=(const D3DApp& rhs) = delete;
    virtual ~D3DApp();
    The parts in bold have confused me. I see a constructor which takes a HINSTANCE and a destructor that can be over-ridden. But what are the other two lines in between those?

    I really haven't got a clue! Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    They are respectively the copy constructor and assignment operator.
    14.14 — Introduction to the copy constructor – Learn C++
    22.3 — Move constructors and move assignment – Learn C++
    21.12 — Overloading the assignment operator – Learn C++

    The = delete means you're forbidding the use of these methods.
    11.4 — Deleting functions – Learn C++

    It's basically to prevent you from (accidentally) creating another copy of your app, using things like
    D3DApp a, b;
    a = b;  // nope!
    D3DApp c(a);  // nope!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Brilliant! Thanks very much Salem I got some reading to do!

  4. #4
    Registered User
    Join Date
    Jan 2010
    I had a good read of that stuff, and also recently came across it on the NetAcad course I'm doing. So no use of copy constructors allowed through either the typical a = b style nor the function a(b) style. Thanks again

  5. #5
    Registered User
    Join Date
    May 2024
    Those lines are actually related to preventing the compiler from generating default copy constructor and copy assignment operator for the D3DApp class. By using = delete, you're essentially telling the compiler not to allow copying of D3DApp objects. This is often done to prevent unintended behavior or resource management issues, especially in classes that manage resources like memory or handles. So, in essence, those lines ensure that instances of D3DApp cannot be copied or assigned to another instance, helping to maintain the integrity of the class's design.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Thanks very much fbdf6, much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 03:01 PM
  2. Class constructors
    By c++.prog.newbie in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2006, 09:15 PM
  3. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  4. help with class and constructors
    By stautze in forum C++ Programming
    Replies: 14
    Last Post: 10-16-2002, 07:56 PM
  5. Constructors of a class
    By casanova0o7 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2002, 03:46 PM

Tags for this Thread