From the storage space point of viewThe virtual function corresponds to a vtable pointer to a virtual function table, which we all know, but this vtable pointer is actually stored in the object's memory space. The problem comes out, if the constructor is virtual, it needs vtable to be called through, but the object has
The reason the constructor cannot be declared as a virtual function:
1, the so-called virtual function is a polymorphic case only executes one, and from the concept of inheritance, always first constructs the parent class object, then can make the child class object, if the constructor is set to the
Why cannot an inline function, constructor, or static member function be a virtual function?
1> inline functions
Inline functions are expanded during compilation, and virtual functions are dynamically integrated during runtime. Therefore, they cannot be defined as virtual functions.
2>
Preface
This article will explain the mistakes that a novice C ++ programmer often makes-using virtual functions in constructor/destructor, and analyzing the cause of the error and how to avoid it.
Error cause
First, let's assume that we use a class that implements transaction as the parent class, then a class that implements purchase, and a class that implements sale as its subclass.
During initialization
Constructor: allocates storage space for an object to initialize it;Destructor: performs the final scanning at the end of the object's life cycle and releases the memory allocated by the constructor;
The constructor cannot be a virtual function:
Your own words: [virtual
function on the blog to see an article "FxCop constructorsshouldnotcallbaseclassvirtualmethods norms," the original: http://blog.csdn.net/xamcsdn2/ Archive/2004/08/11/71766.aspx
Quite interested in writing a piece of code research (the estimate is still not a study, just explore it) a bit.
The code is as follows.
Using System;
Namespace Test_console
{
??? Class BaseClass
??? {
??????? public int count = 0;
??????? Public BaseClass ()
??????? {
??????????? Console.WriteLine ("Call BaseClass
Each time you create a transaction object, you must create an appropriate record in the audit log. The following is a reasonable practice:
Class transaction{Public:Transaction (){Init ();}Virtual void logtransaction () const {STD: cout Protected:
PRIVATE:Void Init (){Logtransaction ();}};
Class buytransaction: Public transaction{Public:Buytransaction (): A (5 ){}Virtual void logtransaction () const {STD: co
Copy constructorIf you do not define a copy constructor, the compiler will automatically generate a copy constructor, but there is a limit to the copy constructor.In general, this copy constructor is directly copied according to the bit, but in some cases this initialization is problematic, in the special four cases is problematic, in case of problems, can be ill
First, I wrote a constructor with the virtual Class A, the code is as follows:Class A{public:virtual A () {}};Run Result: (I was running under VS)650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/8C/4E/wKiom1hp9s6C6s6qAABXFQCBqKI219.png-wh_500x0-wm_3 -wmp_4-s_4041109755.png "title=" 1.png "alt=" Wkiom1hp9s6c6s6qaabxfqcbqki219.png-wh_50 "/>You can see that this code compiles with a problem.Why can't
The following code is compiled with an error because the constructor cannot be a virtual function.1 classStudent {2 Public:3 VirtualStudent ()4 {5 This->dwno =0;6 }7 8 Private:9 intDwno;Ten};So why can't a constructor be a virtual function?Here you need to know a concept, that is, the
Environment: XPSP3 VS2005Today the black general to the candidate out of a C + + constructor call virtual function, the specific topic is more complex than the title, the general situation can see the following code:[CPP]View PlainCopy
Class Base
{
Public
Base ()
{
Fuction ();
}
virtual void fuction ()
{
cout "base::fuction"
}
};
The constructor cannot be a virtual function. When you create a derived class object, the constructor of the derived class is called instead of the constructor of the base class, and then the constructor of the derived class uses a const
//Virtual keyword--a deep understanding of the constructor function#include using namespacestd;/*C language Compiler, C + + compiler is all static chain, is a piece of code compilation, do not consider the context such as compiling to the void Protecta (point pin) compiler will think the pin is the point type, but not to contact the context, To analyze whether a pin might be a subclass object The
Let's look at a section of code that calls a virtual function directly in the constructor:
1 #include 18 ~derive () {delete m_pdata;} virtual void Foo () std::cout
The results here will be printed: 1.
This indicates that line 6th executes Base::foo () instead of Derive::foo (), which means that a virtual
How can c ++ have "virtual Constructor "? What is the return value? Sometimes we want the class constructor to be used like other common member functions: override ). We can design such a constructor function:
We define such a "virtual
Design Mode: factory mode (continued: Virtual constructor and abstract factory)
In the previous design mode: factory mode, two factory modes are recorded for creating a derived class object. The first mode directly uses the static member function of the base class to create the object of the derived class, this static member function directly calls the constructor
;
The normal situation should be output:
12
Do something inclass ClxDerived!Output from the destructor of classClxDerived!
If you define a destructor for a class clxbase as non-virtual (remove the previous virtual), the output is:
1
Do something inclassClxDerived!
There is no destructor to call clxderived Oh ~ ~ ~Also, when do you want
Factory method Mode (Virtual constructor/polymorphic Factory)The factory method pattern is the creation mode of the class, also known as virtual Construction sub-mode (Vsan Constructor) or polymorphism Factory mode (polymorphic Factory).The factory method pattern is intended to define a factory interface that creates p
C + + Class object construction, the first application of a piece of memory, and then call the constructor to initialize;We know that if there is a virtual function, there will be a virtual function table vtable, and the virtual function is when the expression is generated, of course, after the call to the
Be cautious when calling Virtual Methods in the base class constructor.
Recently, a problem was found when calling a virtual method in the base class constructor. First, reproduce the problem as follows:
class Program { static void Main(string[] args) { var c = new Chinese(18);
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.