Declaration and use of C + + base class, derived class object pointers

Source: Internet
Author: User

1. class pointers, object pointers

Class x{

...

Public

Voidshow ();

};

Main ()

{

x X1,*PTR1; A pointer to the object X1 and class X defining Class X Ptr1

x X2,*PTR2; A pointer to the object X2 and Class X defining Class X PTR2

X*PTR3; Defines a pointer to class X PTR3

...

PTR1 =&x1; Point pointer ptr1 to X1 object

PTR2 =&x2; Point pointer ptr2 to X2 object

Ptr1-> Show (); //

Ptr2-> Show (); //

Ptr3-> Show (); Pointer PTR3 directly to the class, calling the class public segment member function

}

2. Introduction of object pointers after derived classes

(1) A pointer to a base class object points to its public-derived object, pointing to a private derived object error;

eg

Class base{

...

};

Class derive:base{

...

};

Main ()

{

Base Obj1, *PTR1;

derive = Obj2;

PTR1 = &obj1;

PTR1 = &obj2; Error, pointing to a private derived class object

}

(2) A pointer to a derived class object cannot point to the object of the base class;

eg

Main ()

{

Base Obj1, *PTR1;

Derive Obj2, *PTR2;

PTR2 = *obj2; Point the pointer ptr to a derived class object Obj2

PTR2 = *obj1; Error

...

}

(3) A pointer to a base class object that, when pointed to a derived class object, can only point directly to a member in the derived class that inherits from the base class and does not have direct access to a particular member of the derived class;

(4) But other methods can be used to implement pointers to specific members in derived classes

eg

Class base{

...

Public

Voidshow ();

Voidshow1 ();

};

Class Derive:public base{

...

Public

Voidshow ();

Voidshow2 ();

};

Main ()

{

Base Obj1, *PTR1;

Derive Obj2, *PTR2;

PTR1 = &obj1; The pointer points to the base class object Obj1

PTR1, show (); Call the public segment member function of base class base show

PTR1 = &obj2; The pointer points to the derived class object Obj2

PTR1, show (); Calling a derived class object obj2 a public segment member function inherited from a base class show

((derive *) ptr1), show (); Calling a specific member function of a derived class object Obj2 show ()

((derive *) ptr1), Show2 (); Calling a derived class object obj2 a specific member function Show2 ()

PTR1 = &obj1; The pointer re-points to the base class object Obj1

PTR1, Show1 (); Call the base class public segment member function Show1 ()

}

Declaration and use of C + + base class, derived class object pointers

Related Article

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.