Member selectors in C + +

Source: Internet
Author: User

In C + +, pointers that can only point to class members can be used to extract data from this type of pointer, using the following two types of operators: the member object selection operator. * and member pointer selection operator->*

Example:

#include <iostream>using namespace std;struct c{    int x;    float y;    float Z;}; int main () {    float F;    int* i_ptr;    C C1, C2;    Float C::* f_ptr; A pointer to a float member of C    f_ptr = &C::y;  Point to C member y    c1.*f_ptr = 3.14;//Set c1.y = 3.14    c2.*f_ptr = 2.01;//Set c2.y = 2.01    f_ptr = &am P C::z;  Point to C member Z    c1.*f_ptr = -9999.99,//set c1.z = -9999.99    f_ptr = &C::y;//point to c member y
  
   c1.*f_ptr =-777.77; Set c1.y    f_ptr = &C::x;//****error:x is not float****    f_ptr = &f;//****error:f are not a float m Ember of c****    i_ptr = &c1.x;//c1.x is an int    //...    return 0;}
  

  

Member selectors in C + +

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.