Thread: Question about strange constructors in class

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

    Question Question about strange constructors in class

    Hi there!

    Have a look at the following code in a class:

    Code:
    class D3DApp
    {
    protected:
    
    D3DApp(HINSTANCE hInstance);
    D3DApp(const D3DApp& rhs) = delete;
    D3DApp& operator=(const D3DApp& rhs) = delete;
    virtual ~D3DApp();
    
    ....omitted
    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
    Location
    The edge of the known universe
    Posts
    39,661
    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
    Code:
    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
    Posts
    206
    Brilliant! Thanks very much Salem I got some reading to do!

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    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

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