C ++ Starting question: Constructor

I just stumbled upon the following constructor source code.

testProg::testProg() : CCProg()  
{  
m_UID = n_UID = 0;  
}  

Usually the constructor looks in accordance with my understanding:

testProg::testProg()    
{    
m_UID = n_UID = 0;    
}    

So, I wonder what the purpose of this CCProg () is, it would be great if someone could quickly tell me what is going on here. Thanks!

+3
source share
4 answers

It would seem that testProg inherits from CCProg, and the no-args constructor for CCProg is called from the initialization list of the testProg constructor.

, no-args, ( ). , .

:

testProg::testProg(int days) : CCProg(days)  
{  
m_UID = n_UID = 0;  
}

, , no-args , , .

, ( ), CCProg - -, testProg, - , no-args , implicity.

+16

, :

class testProg : public CCProg

( ) -, :

class testProg
{
    Something CCProg;
    // ...
+6

:

  • testProg CCProg,
  • testProg CCProg

CCProg, , , .

, CCProg int, -

testProg::testProg() : CCProg(1) { }

( , CCProg 1) . , : CCProg testprog.

+4

, , CCProg() .

In the inheritance architecture, we can use this technique to save the effort of rewriting the base class constructor.

0
source

Source: https://habr.com/ru/post/1713007/


All Articles