Home > Backend Development > C++ > body text

How does polymorphism support delegation and event handling in C++?

WBOY
Release: 2024-06-03 16:58:01
Original
766 people have browsed it

C++ polymorphism is implemented through virtual functions and dynamic binding, supporting delegation and event handling. Delegates allow objects to call base class methods through subclass objects, often used in callback functions. Event handling uses delegates to execute code when a specific event occurs. This article provides two examples: the delegate example shows a subclass function being called through a base class pointer, and the event handling example handles button click events through a button listener.

C++ 中多态性如何支持委托和事件处理?

Polymorphism in C++: Delegation and event handling

Polymorphism is an important aspect of object-oriented programming Attribute that allows calling base class methods through subclass objects. In C++, polymorphism is achieved through virtual functions and dynamic binding.

Delegation

Delegation is an inter-object communication mechanism that allows an object to delegate certain tasks or behaviors to another object. Delegates are often used to implement callback functions, where one object calls a function on another object and waits for its response.

The following C++ code example shows how to use polymorphism to implement delegation:

#include <iostream>

using namespace std;

class Base {
public:
  virtual void print() const { cout << "Base" << endl; }
};

class Derived : public Base {
public:
  void print() const override { cout << "Derived" << endl; }
};

int main() {
  Base* b = new Derived();  // 多态性:Base 指针可以指向 Derived 对象
  b->print();  // 调用派生类函数
  return 0;
}
Copy after login

Event handling

Event handling involves executing when a specific event occurs on an object some code. In C++, event handling can be implemented using delegates.

The following C++ code examples show how to use polymorphism to implement event handling:

#include <iostream>

using namespace std;

class Button {
public:
  void click() const { if (_listener) _listener->buttonClicked(); }
  void setListener(ButtonListener* listener) { _listener = listener; }
private:
  ButtonListener* _listener = nullptr;
};

class ButtonListener {
public:
  virtual void buttonClicked() {}
};

class Form {
public:
  Form() : _button(new Button()) {}
  void setButtonListener(ButtonListener* listener) { _button->setListener(listener); }
private:
  Button* _button;
};

class MyListener : public ButtonListener {
public:
  void buttonClicked() override { cout << "Button clicked!" << endl; }
};

int main() {
  Form form;
  form.setButtonListener(new MyListener());
  form._button->click();
  return 0;
}
Copy after login

Hope these examples help you understand how polymorphism supports delegation and event handling in C++.

The above is the detailed content of How does polymorphism support delegation and event handling in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template