C + + Object memory distribution (1)-Private virtual function

Source: Internet
Author: User

1. Preface

For all the code examples in this article, if you are running on Windows compilation, you are using Visual Studio 2013. If the RHEL6.5 platform (Linux kernal:2.6.32-431.el6.i686) is compiled and run, its GCC version is 4.4.7 as follows:
[Email protected] ~]# gcc--version
GCC (gcc) 4.4.7 20120313 (Red Hat 4.4.7-4)

2. Non-public virtual functions

Before you begin to explore the specific distribution of memory in an object, consider some of the behavior of virtual functions in C + +.

2.1. Public virtual Functions

It is most common for subclasses to achieve polymorphism by overriding public virtual functions. The base-class pointer to the subclass object calls the function overridden by the quilt class, in effect, a function called a subclass.

Refer to the following code:

#include <iostream>class base{public:virtual void foo () {std::cout << "Base::foo" << Std::endl;}}; Class Derive:p ublic base{void foo () {std::cout << "Derive::foo" << Std::endl;}}; int main () {Base *PB = new Derive ();p B->foo (); return 0;}
There is no doubt that although Foo is privately private by default in subclasses, the output will be as follows:

Derive::foo

Specific reasons have been discussed in the previous C + + series articles. Refer to this article "C + + virtual function (9)-virtual function can be private?".

2.2. Private virtual Functions

In contrast to the public virtual function is to protect virtual functions and private virtual functions, here, in order to facilitate the discussion of the problem, only discuss the private virtual function. As for the protection of virtual functions and private virtual functions are similar, the difference is only the function of the visibility of different.

On the surface, a virtual function is intended to enable the parent class pointer to access the function of the child class object. If a virtual function is set to private, the function cannot be accessed regardless of the child class object or the parent object. Such a function becomes meaningless. Is it true? The answer is in the negative. First, C + + supports private virtual functions, of course, also supports the protection of virtual functions, here, the protection of virtual function is not discussed.

Design pattern has the design principle of encapsulation algorithm. The principle uses the template method pattern, which provides an externally accessible interface that clarifies the steps of the algorithm. For each step of the algorithm, it is specified by the external inheritance method.

Here is a simple example to illustrate.

As we all know, the process of shopping is to select goods first, then pay, and finally leave the store. In this process, the 3 steps are sequential and cannot be reversed in a random order. In life, this is a lot of similar examples. The following writing is an abstract class for shopping, which has 3 private pure virtual functions choose (), Pay () and leave (), which represent the selection of items for shopping, payment, and 3 procedures for leaving the store, which are called by the shopping function in turn to the 3 functions:

#include <iostream>class store{public:void Shopping () {std::cout << "base::shopping" << Std::endl; Choose ();p ay (); leave ();} private:virtual void Choose () = 0;virtual void pay () = 0;virtual void leave () = 0;};/ /implements a class that represents the purchase of vegetables, which inherits from the store class, overwriting the Choose (), Pay (), and leave () functions in the store class. Class Vegetablestore:p ublic store{private:void Choose () {std::cout << vegetablestore::choose << std:: Endl;} void Pay () {std::cout << "Vegetablestore::p ay" << std::endl;} void Leave () {std::cout << "vegetablestore::leave" << Std::endl;}}; int main () {Store *ps = new Vegetablestore ();p s->shopping (); return 0;}

Output:

Base::shopping
Vegetablestore::choose
Vegetablestore::p ay
Vegetablestore::leave

As you can see from the code above, the parent store prescribes the process of shopping through the shopping function, as to what each step of shopping does, as specified by the subclass. Since the function that represents the shopping step is declared private, we cannot call choose (), Pay () or lease () through the subclass object to avoid the confusion of leaving the product first and then picking the goods.

C + + Object memory distribution (1)-Private virtual function

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.