C + + object Model Learning notes

Source: Internet
Author: User

1. How the global variables are initialized
// Global Var A; int Main () {    cout<<a<<Endl;     return 0 ;  

As the above example, the global variable A is initialized before the main () function, but how is it initialized? the answer is called static initialization !

2. C + + package layout cost

The main additional burden of C + + in layout and access time is caused by the virtual mechanism, including:

virtual function mechanism: used to support efficient "execution-time bindings"

Virtual base class: A single, shared entity that implements "multiple occurrences of the base class in the inheritance system"

In addition, there are additional burdens under multiple inheritance, which occur between "a derived class and its second or subsequent base class conversion". In general, however, there is no natural reason to say that C + + programs must be larger or slower than the C-brothers.

3. What is the difference between a parent class and a child class pointer?

Look at the following example:

classzooanimal{ Public: Zooanimal (): Name ("") {cout<<"Zooanimal::zooanimal ()"<<Endl; } zooanimal (stringstr): Name (str) {cout<<"zooanimal::zooanimal (String)"<<Endl; }    Virtual~Zooanimal () {cout<<"Zooanimal::~zooanimal ()"<<Endl; }    Virtual voidrotate () {cout<<"zooanimal::rotate ()"<<Endl; }protected:    intLoc; stringname;};classBear: Publiczooanimal{ Public: Bear () {cout<<"bear::bear ()"<<Endl; } Bear (stringstr): Zooanimal (str) {cout<<"bear::bear (String)"<<Endl; }    ~Bear () {cout<<"bear::~bear ()"<<Endl; }    voidrotate () {cout<<"bear::rotate ()"<<Endl; }    Virtual voidDance () {cout<<"Bear ::d ance ()"<<Endl; }protected:    enumDances{dan, SLEEP};    Dances Dances_known; intCell_block;};intMain () {cout<<"sizeof (zooanimal):"<<sizeof(Zooanimal) <<Endl; cout<<"sizeof (Bear):"<<sizeof(Bear) <<Endl; Bear B ( "Yogi");    Bear *PB = &b;        Zooanimal *PBR = &b; return 0;}

What is the difference between the pointer Pb and PBR? They all point to the first byte of the Bear object B, where the difference is that PB covers an address that contains the entire bear object, and the address covered by PBR contains only the zooanimal portion of the.

C + + object Model Learning notes

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.