Can a virtual function be an inline function?

Source: Internet
Author: User

Inline functions cannot be virtual functions because the virtual table mechanism requires a real function address. After an inline function is expanded, it is not a function, but a simple one.Code(Most C ++ object models use virtual tables to implement polymorphism, which is supported by this standard.) Some inline functions may not be able to expand inline and compile them into functions.

Class foo
{
Public:
Virtual void vfunc () const;
PRIVATE:
};

Class child: Public foo
{
Public:
Virtual void vfunc () const;
};

Inline
Void FOO: vfunc () const
{Cout <"class foo" <Endl ;}

Inline
Void child: vfunc () const
{
Cout <"class child" <Endl;
}

 

Int main ()
{
Foo * PCH = new child;
PCH-> vfunc ();
 
Return 0;
}

 

Output is child! Even if the virtual function is an inline function, it is still correctly bound dynamically!

The reason is: even if a virtual function is declared as an inline function, the compiler will not expand such a function in inline mode, but process it as a normal function.

 


Constructors cannot be virtual functions. Virtual functions are implemented by using the same function name in the inherited class. The constructor does not need or can make you rtti! (The subclass name cannot be the same as the parent class name)

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.