Method for private member variables in the C ++ variables class

Source: Internet
Author: User
Tags gety

In principle, private variables in the C ++ class are not allowed to be accessed anywhere outside of the class. Generally, a fully functional class provides the get and set methods to operate class attribute values, there is also access through youyuan. But! However, if no get or set method is provided, no friends are defined. For example, a third party provides a set method. O (or dynamic library) for development, and in actual applications, we actually need to change a private parameter of an object. Is there any way? There is also a method of comparing literature and art young people. We know that a process has a program segment and a data segment. If we know the object's data space, it is easy to get the member variable value of the object, in fact, the first address of the object data segment is actually the object address. For example:

Method 1: ordinary youth

Youyuan method:

# Include <iostream. h> Class A {int t; public: A () {T = 2002; // initialize the member variable t} friend Class B in the constructor; // declare a friend class classb}; Class B {public: A; int sum () {return. T + 1500; // member Class B accesses the private member variable t} in Class A; void main () {B; cout <B. sum () <Endl ;}

Method 2: literary youth

Memory Address Operation Method

We know that the C ++ compiler separates data from program segments, and all class variables are stored in data segments in order of declaration. Therefore, if you know the address of the first variable, then the following addresses are accumulated one by one. With the variable address, you can modify its value. The preceding example shows how to change the value of class member B:

# Include <iostream> # include <string> using namespace STD; class center {public: void setx (float _ x) {x = _ x;} void sety (float _ y) {Y = _ y;} void setmeanvalue (float avg) {meanvalue = AVG;} float getx () {return X;} float Gety () {return y ;} float getmeanvalue () {return meanvalue;} Center (): X (0.0), y (0.0), meanvalue (0.0) {} private: Float X; float y; float meanvalue ;}; int main () {center a; // assign a value to a common youth.. setx (1.0);. sety (4.0);. setmeanvalues (13.0); cout <. getx () <"" <. gety () <"" <. getmeanvalue () <Endl; // Method for assigning values to young artists; // * (float *) & ): forcibly convert the memory space of the Center Object A to the memory space pointed to by int *, and access the memory float TMP = * (float *) & ); cout <TMP <Endl; // Output. the value of x tmp = * (float *) & A + 1); cout <TMP <Endl; // Output. value of y * (float *) & A + 2) = 2; // modify. meanvalue value cout <. getmeanvalue () <Endl ;}

You can also use the memory address of youyuan to access private members of Class A through the memory address of Class B.


In addition, an article similar to this is also enlightening.

Analyze the differences between programmers and hackers
Question:
The following C ++ class is provided:

class A{  int value;public:  A(int n = 0) : value(n) {}  int GetValue()  {    return value;  }};

Please use some method to change the value of private member A: value outside the class.
Possible practices of programmers:

class A{  int value;public:  A(int n = 0) : value(n) {}  int GetValue()  {    return value;  }  void SetValue(int n)  {    value = n;  }};void f(){  A a;  a.SetValue(5);}

Possible hacker practices:

void f(){  A a;  *((int *)&a) = 5;}

Conclusion:
Programmers are used to adding existing things by following existing restrictions.
Hackers are used to breaking existing restrictions by using existing things.

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.