A private member of C + + that accesses an object inside a class

Source: Internet
Author: User

First, Introduction

Looking at a piece of code found in the project today, it is possible to re-access the private members of the class's object in the class's member function. It feels incredible.

To write your own instance code:

#include <iostream>using namespacestd;classctest{ Public: CTest (intN) {m_a=N; } ctest (CTest&t) {m_a=T.m_a; } Public:    voidPrint () {cout<< M_a <<Endl; }Private:    intm_a;};intMainintargcChar**argv) {CTest T (Ten);    CTest T2 (t);    T.print (); T2.    Print (); return 0;}

This code can be run correctly.

Ii. Conclusion

When learning, only remember that private members of a class cannot be accessed externally. So it is considered absolutely impossible to access the object T.m_a in a way that can actually be used. It is accessed inside the class, although it is inside another object.

So how does it come about that the private members of the object can be accessed in other objects of the same kind, but not accessible in objects of different classes?

A private member of C + + that accesses an object inside a class

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.