09 can constructors call virtual functions?

Source: Internet
Author: User

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/whether-constructor-can-call-virtual-function.html

[Question]

Can constructors call virtual functions? Is it syntactically passed? Can it be passed in semantics?

[Analysis]

The constructor calls virtual functions. The syntax can be passed (the program can be executed normally), but the semantics is not passed (the execution result is not what we want)

See the following code

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/9/29
*/

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;


Class base
{
Public:
Base ()
{
Foo ();
}

Virtual void Foo ()
{
Cout <"base: foo" <1 <STD: Endl;
}
};

Class derived: public Base
{
Public:
Derived (): Base (), m_pdata (New int (2 )){}
~ Derived ()
{
Delete m_pdata;
}

Virtual void Foo ()
{
Cout <"derived: foo" <* m_pdata <STD: Endl;
}
PRIVATE:
Int * m_pdata;
};

Void test ()
{
Base * P = new derived ();
Delete P;
}

Int main ()
{
Test ();
Return 0;
}
/*
Base: Foo 1
*/

The execution result is base: Foo 1.

This indicates that row 19th executes base: Foo () instead of derived: Foo (), that is, the virtual function "does not work" in the constructor ". Why?

When instantiating a derived class object, first construct the base class part, and then construct the derived class part. That is, when a derived object is created, the base constructor is called before the derived constructor is called. When constructing the base class, the derived class has not been fully created. In a sense, it is only a base class object. That is, when base: Base () is executed, the derive object has not been completely created. In this case, it is regarded as a base object rather than a derive object. Therefore, foo is bound to the base Foo.

C ++ is designed to reduce errors and bugs. Assume that the virtual function still "takes effect" in the constructor, that is, Foo () in base: Base (); the call is derive: Foo (). When base: Base () is called, m_pdata In the derived class has not been correctly initialized. Executing derive: Foo () will cause the program to unreference an uninitialized address, the result is unpredictable, or even a program crash (access to illegal memory ).

In summary, the base class is constructed before the derived class. When the base class constructor is executed, the data members in the derived class are not initialized. If the call to the virtual function in the base class constructor is parsed to call the virtual function of the derived class, and the virtual function of the derived class accesses the uninitialized data of the derived class, this will cause some undefined behaviors and bugs in the program.

The constructor directly calls the pure virtual function (pure virtual function). An error is returned during compilation: unresolved externals.

[Code]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/9/29
*/

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Class base
{
Public:
Base ()
{
Foo ();
}

Virtual void Foo () = 0; // pure virtual function
};

Class derived: public Base
{
Public:
Derived (): Base (), m_pdata (New int (2 ))
{
}
~ Derived ()
{
Delete m_pdata;
}

Virtual void Foo ()
{
Cout <"derived: foo" <* m_pdata <STD: Endl;
}
PRIVATE:
Int * m_pdata;
};

Void test ()
{
Base * P = new derived ();
Delete P;
}

Int main ()
{
Test ();
Return 0;
}

If the compiler can identify such wrong calls during compilation or linking, the chances of making mistakes will be greatly reduced. The compiler cannot determine the cause of the problem. In this case, it can generate executable files, but an error occurs when the program is running, because base: Foo only declares that it is not defined.

Indirect call of ConstructorPure virtual functions (Pure virtual function) (I .e., constructor callCommon functions, but common functions call pure virtual functions again). During the compilation stage, no error is reported. executable files can be generated, but errors may occur because pure virtual functions are not defined.

[Code]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/9/29
*/

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Class base
{
Public:
Base ()
{
Function ();
}

Void function ()
{
Foo (); // normal function CILS pure virtual function
}

Virtual void Foo () = 0; // pure virtual function
};

Class derived: public Base
{
Public:
Derived (): Base (), m_pdata (New int (2 ))
{
}
~ Derived ()
{
Delete m_pdata;
}

Virtual void Foo ()
{
Cout <"derived: foo" <* m_pdata <STD: Endl;
}
PRIVATE:
Int * m_pdata;
};

Void test ()
{
Base * P = new derived ();
Delete P;
}

Int main ()
{
Test ();
Return 0;
}
Conclusion: You should never call a virtual function during the class construction or destructor, because such a call will never be passed down to the subclass along the class inheritance tree.

[Reference]

Http://www.cnblogs.com/carter2000/archive/2012/04/28/2474960.html

Http://blog.csdn.net/hxz_qlh/article/details/14089895

09 can constructors call virtual functions?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.