Thread: Inherited Class Constructor - with an unusual description?

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

    Smile Inherited Class Constructor - with an unusual description?

    Hi there!

    I'm working through a demo application for my DX12 studying and I've come across this. There's a parent class and a child class, and the child class is simply declared as inheriting from the parent class, as it usually is.

    However there's a weird extra line in there that to me seems to reference the parent class unnecessarily. Have a look:

    Code:
    class InitDirect3DApp : public D3DApp
    {
    public:
    	InitDirect3DApp(HINSTANCE hInstance);
    	~InitDirect3DApp();
    
    	...more stuff
    };
    So there's the class. Now take a look at its constructor:

    Code:
    InitDirect3DApp::InitDirect3DApp(HINSTANCE hInstance)
    : D3DApp(hInstance) 
    {
          ...empty definition?
    }
    The part in bold : D3DApp(hInstance) seems unnecessary to me, in fact I don't even know what it's doing. I thought you simply declare a class as inheriting from another, and then write a specific constructor for it without having to reference its parent class at all in that definition.

    Furthermore I don't understand why the definition is empty and yet still references the parent class. To be honest I don't even understand the syntax involved. How does: D3DApp(hInstance) at the end of the definition even get handled by the compiler?

    I'm pretty stumped and would be glad of the help thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    The idea is to invoke the base class constructor that takes a HINSTANCE, passing hInstance as the argument. If you didn't do that, then the default constructor for the base class would be invoked instead, and this would either be the wrong behaviour or result in a compile error if that constructor doesn't exist.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    228
    Interesting! Thanks for the reply, I guess I need dig into some code and see exactly what's going on with the constructors, thanks very much

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    228
    Sure enough you were absolutely right. The base class has 2 constructors and the one this code invokes is the correct one! Thanks very much

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    1
    The constructor you used here is a parameterized constructor. In this constructor an argument is passed which makes it parametrized constructor. You have to go through the difference between default and parameterized constructors by looking at some programs using these two and observing the changes in them. That'll be helpful to you.

  6. #6
    Registered User
    Join Date
    Jun 2022
    Posts
    1
    D3DApp(hInstance)its a base class parameterized constructor, hInstance as a argument. If you didn't invoke the base class contructor, then the default constructor for the base class would be invoked instead.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    228
    Hey thanks very much Shivam18 and sakshijn Deepened my knowledge even further thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherited classes cannot call constructor
    By Befiscure45 in forum C++ Programming
    Replies: 9
    Last Post: 04-16-2019, 09:19 PM
  2. Inherited class constructor
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2013, 04:30 PM
  3. im trying to pass an object to an inherited class
    By kdun in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2013, 02:49 AM
  4. how to make an inherited constructor private?
    By thedodgeruk in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2011, 11:05 AM
  5. Replies: 3
    Last Post: 03-26-2006, 12:59 AM

Tags for this Thread