C++ 虚函数
C++ virtual function is, polymorphism(多态)
* A member function of a class
* Declared with virtual keyword
* Usually has a different functionality in the derived class
* A function call is resolved at run-time
The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding(绑定). Where as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.
C++ Virtual Function - Reasons:
The most prominent reason why a C++ virtual function will be used is to have a different functionality in the derived class.
For example a Create function in a class Window may have to create a window with white background. But a class called CommandButton derived or inherited from Window, may have to use a gray background and write a caption on the center. The Create function for CommandButton now should have a functionality different from the one at the class called Window.
C++ Virtual function - Example:
This article assumes a base class named Window with a virtual member function named Create. The derived class name will be CommandButton, with our over ridden function Create.
#include <iostream.h>
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"<<endl;
}
};
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
}
};
class background:public Window
{
public:
void Create()
{
cout<<"this is another derive class -overridden c++ virtual function"<<endl;
}
};
int main()
{
Window *x, *y,*z;
x = new Window();
x->Create();
y = new CommandButton();
y->Create();
z=new background();
z->Create();
delete x,y,z;
return 0;
}
The output of the above program will be,
Base class Window
Derived class Command Button - Overridden C++ virtual function
this is another derive class -overridden c++ virtual function
If the function had not been declared virtual, then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.
C++ Virtual function - Call Mechanism:
在多重继承中是一样的
Virtual Constructors and Destructors
A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.
没有虚构造函数,有虚析构函数
A virtual destructor is one that is declared as virtual in the base class and is used to ensure that destructors are called in the proper order. It is to be remembered that destructors are called in the reverse order of inheritance. If a base class pointer points to a derived class object and we some time later use the delete operator to delete the object, then the derived class destructor is not called. Refer to the code that follows:
如果基类的析构函数只作为成员函数,则派生类的析构函数将不会被调用
#include <iostream.h>
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"<<endl;
}
~Window()
{
cout<<"the Window destructor"<<endl;
}
};
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
}
~CommandButton()
{
cout<<"the CommandButton destructor "<<endl;
}
};
class background:public Window
{
public:
void Create()
{
cout<<"this is another derive class -overridden c++ virtual function"<<endl;
}
~background()
{
cout<<"the background destructor"<<endl;
}
};
int main()
{
Window *x, *y,*z;
x = new Window();
x->Create();
y = new CommandButton();
y->Create();
z=new background();
z->Create();
//delete x,y,z; delete one, hence, don't do like this
delete x;
delete y;
delete z;
return 0;
}
the output of above programme is:
Base class Window
Derived class Command Button - Overridden C++ virtual function
this is another derive class -overridden c++ virtual function
the Window destructor
the Window destructor
the Window destructor
将Window类的析构函数变为虚类后,派生类的析构就能调用了,修改如下:
virtual ~Window()
{
cout<<"the Window destructor"<<endl;
}
now, the output of above programme is:
Base class Window
Derived class Command Button - Overridden C++ virtual function
this is another derive class -overridden c++ virtual function
the Window destructor
the fist derived delete
the Window destructor
the second drived delete
the Window destructor
现在,那何时使用虚析构器呢,只要有虚函数存在的时候,一般就应该用虚析构器。
对象切片问题
#include <iostream.h>
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"<<endl;
}
virtual ~Window()
{
cout<<"the Window destructor"<<endl;
}
};
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
}
~CommandButton()
{
cout<<"the CommandButton destructor "<<endl;
}
};
class background:public Window
{
public:
void Create()
{
cout<<"this is another derive class -overridden c++ virtual function"<<endl;
}
~background()
{
cout<<"the background destructor"<<endl;
}
};
void f(Window b) //this is append it will produce slice question
{
b.Create();
}
int main()
{
Window *x, *y,*z;
x = new Window();
x->Create();
y = new CommandButton();
y->Create();
z=new background();
z->Create();
background aa;
f(aa);
//delete x,y,z; delete one, hence, don't do like this
delete x;
delete y;
delete z;
return 0;
}
********the output*******
Base class Window
Derived class Command Button - Overridden C++ virtual function
this is another derive class -overridden c++ virtual function
Base class Window //invoking the Window class, don't invoking the background class
the Window destructor
*******
如果将上面的void f(Window b) 改为如下的形式
void f(Window &b) //this is append it will produce slice question
{
b.Create();
}
或者是
void f(Window *b) //this is append it will produce slice question
{
b->Create();
}
其结果为
Base class Window
Derived class Command Button - Overridden C++ virtual function
this is another derive class -overridden c++ virtual function
this is another derive class -overridden c++ virtual function //invoking the background class
the Window destructor
******
为了防止这种现象的出现,就是不要使用值传递的方式来进行向上映射,或尽可能地使用引用或指针来进行向上映射
纯虚函数
格式:virtual <类型><函数名>(<参数列表>)=0
当在一个类中定义了纯虚函数后,传值调用时就会出错,只能使用引用或指针来进行。含有虚函数的类称为抽象类
将上面的程序修改一下,就将Window变为抽象类。代码如下:
#include <iostream.h>
class Window // Base class for C++ virtual function example
{
public:
virtual void Create()=0; // virtual function for C++ virtual function
example
//{
// cout <<"Base class Window"<<endl;
// }
virtual ~Window()
{
cout<<"the Window destructor"<<endl;
}
};
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
}
~CommandButton()
{
cout<<"the fist derived delete"<<endl;
}
};
class background:public Window
{
public:
void Create()
{
cout<<"this is another derive class -overridden c++ virtual function"<<endl;
}
~background()
{
cout<<"the second drived delete"<<endl;
}
};
void f(Window &d)
{
d.Create();
}
int main()
{
Window *x, *y,*z;
background aa;
// x = new Window(); abstract class have not own object
// x->Create();
y = new CommandButton();
y->Create();
z=new background();
z->Create();
f(aa);
// delete x; 不能多删,如果多删了,会发生内存泄漏
delete y;
delete z;
return 0;
}
动态分配就是在一个堆中分配空间
virtual function and abstract class
最新推荐文章于 2021-04-01 22:23:28 发布