以下是静态绑定(bind)还是动态绑定(bind)的情况?因为我知道,我的指针在编译时指向的对象的类是什么,我的猜测是它是静态的......
class Base
{
void print() const;
};
class Derived : public Base
{
void print() const;
};
int main()
{
Base base;
Base *basePtr = &base;
basePtr->print();
}
为什么我们应该使用动态绑定(bind)而不是静态绑定(bind)?我不明白为什么我们应该使用虚函数而不是简单地以这种方式调用函数 base.print()
(对于上面的例子)。此方法已经“理解”哪个 print()
函数为对象的正确类调用哪个函数,而无需使用多态性和虚函数。
更新
// Point class definition represents an x-y coordinate pair.
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point( int = 0, int = 0 ); // default constructor
void setX( int ); // set x in coordinate pair
int getX() const; // return x from coordinate pair
void setY( int ); // set y in coordinate pair
int getY() const; // return y from coordinate pair
virtual void print() const; // output Point object
private:
int x; // x part of coordinate pair
int y; // y part of coordinate pair
}; // end class Point
#endif
// Circle class contains x-y coordinate pair and radius.
#ifndef CIRCLE_H
#define CIRCLE_H
#include "point.h" // Point class definition
class Circle : public Point {
public:
// default constructor
Circle( int = 0, int = 0, double = 0.0 );
void setRadius( double ); // set radius
double getRadius() const; // return radius
double getDiameter() const; // return diameter
double getCircumference() const; // return circumference
double getArea() const; // return area
virtual void print() const; // output Circle object
private:
double radius; // Circle's radius
}; // end class Circle
#endif
// Introducing polymorphism, virtual functions and dynamic
// binding.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include "point.h" // Point class definition
#include "circle.h" // Circle class definition
int main()
{
Point point( 30, 50 );
Point *pointPtr = 0;
Circle circle( 120, 89, 2.7 );
Circle *circlePtr = 0;
// set floating-point numeric formatting
cout << fixed << setprecision( 2 );
// output objects point and circle using static binding
cout << "Invoking print function on point and circle "
<< "\nobjects with static binding "
<< "\n\nPoint: ";
point.print(); // static binding
cout << "\nCircle: ";
circle.print(); // static binding
// output objects point and circle using dynamic binding
cout << "\n\nInvoking print function on point and circle "
<< "\nobjects with dynamic binding";
// aim base-class pointer at base-class object and print
pointPtr = &point;
cout << "\n\nCalling virtual function print with base-class"
<< "\npointer to base-class object"
<< "\ninvokes base-class print function:\n";
pointPtr->print();
// aim derived-class pointer at derived-class
// object and print
circlePtr = &circle;
cout << "\n\nCalling virtual function print with "
<< "\nderived-class pointer to derived-class object "
<< "\ninvokes derived-class print function:\n";
circlePtr->print();
// aim base-class pointer at derived-class object and print
pointPtr = &circle;
cout << "\n\nCalling virtual function print with base-class"
<< "\npointer to derived-class object "
<< "\ninvokes derived-class print function:\n";
pointPtr->print(); // polymorphism: invokes circle's print
cout << endl;
return 0;
} // end main
最佳答案
Is the following a case of static, or dynamic binding?
静态,因为该函数不是虚拟的。
如果它是虚拟的,那么该函数将根据对象的动态类型进行调度。
And why we should use dynamic binding over static?
当我们想要通过公共(public)基类与不同类型的对象交互时,而不知道实际(动态)类型是什么。例如,
class Base
{
virtual void print() const; // add "virtual" to enable dynamic dispatch
};
// This function doesn't know the real (dynamic) type of the object,
// but still calls the correct version of "print".
void print(Base const & base) {
// If "print" is virtual, then this calls the override for the dynamic type.
// Otherwise, this calls Base::print.
base.print();
}
int main() {
Base base;
Derived derived;
print(base); // calls Base::print
print(derived); // calls Derived::print
}
This method already "understand" which print() function call for the right class of the object, without using polymorphism and virtual functions.
事实上,如果您已经知道动态类型(如您的示例所示),则多态性没有用。它适用于您不知道动态类型的情况,如我的示例所示。
关于c++ - 这是动态绑定(bind)还是静态绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23542663/