📜  我们可以在不使用 C++ 中的成员或朋友函数的情况下访问类的私有数据成员吗?

📅  最后修改于: 2022-05-13 01:55:07.909000             🧑  作者: Mango

我们可以在不使用 C++ 中的成员或朋友函数的情况下访问类的私有数据成员吗?

封装的想法是将数据和方法(对数据起作用)捆绑在一起,并限制类外部私有数据成员的访问。在 C++ 中,友元函数或友元类也可以访问私有数据成员。

那么,是否可以在没有朋友的情况下访问班级外的私人成员?是的,可以使用指针。虽然它是 C++ 中的一个漏洞,但可以通过指针来实现。

示例 1:

CPP
// CPP Program to initialize the private members and display
// them without using member functions
  
#include 
using namespace std;
  
class Test {
private:
    int data;
  
public:
    Test() { data = 0; }
    int getData() { return data; }
};
  
int main()
{
    Test t;
    int* ptr = (int*)&t;
    *ptr = 10;
    cout << t.getData();
    return 0;
}


CPP
// CPP Program to initialize the private members and display
// them without using member functions
#include 
using namespace std;
  
class A {
private:
    int x;
    int y;
};
  
// Driver Code
int main()
{
    A a;
    int* p = (int*)&a;
    *p = 3;
    p++;
    *p = 9;
    p--;
    cout << endl << "x = " << *p;
    p++;
    cout << endl << "y = " << *p;
    return 0;
}


CPP
// CPP Program to initialize and
// display private members
// using pointers
#include 
using namespace std;
  
class A {
private:
    int x;
    int y;
};
  
class B : public A {
public:
    int z;
  
    void show(int* k)
    {
        cout << "x = " << *k << " y = " << *(k + 1)
             << " z = " << *(k + 2);
    }
};
  
int main()
{
    // object declaration
    B b;
    
    // pointer declaration
    int* p;
    
    // address of z is assigned to p
    p = &b.z;
    
    // initialization of z
    *p = 3;
    
    // points to previous location
    p--;
    
    // initialization of y
    *p = 4;
    
    // points to previous location
    p--;
    
    // initialization of x
    *p = 5;
    
    // passing address of x to function show()
    b.show(p);
  
    return 0;
}


输出
10

示例 2:

CPP

// CPP Program to initialize the private members and display
// them without using member functions
#include 
using namespace std;
  
class A {
private:
    int x;
    int y;
};
  
// Driver Code
int main()
{
    A a;
    int* p = (int*)&a;
    *p = 3;
    p++;
    *p = 9;
    p--;
    cout << endl << "x = " << *p;
    p++;
    cout << endl << "y = " << *p;
    return 0;
}
输出
x = 3
y = 9

说明:在上述程序中,a 是 A 类的对象。通过类型转换将对象的地址分配给整数指针 p。指针 p 指向私有成员x。整数值赋给*p,即x。对象 a 的地址增加并且通过访问内存位置值 9 分配给 y。 p– 语句设置 x 的内存位置。使用cout语句包含的 x 被显示。

示例 3:

CPP

// CPP Program to initialize and
// display private members
// using pointers
#include 
using namespace std;
  
class A {
private:
    int x;
    int y;
};
  
class B : public A {
public:
    int z;
  
    void show(int* k)
    {
        cout << "x = " << *k << " y = " << *(k + 1)
             << " z = " << *(k + 2);
    }
};
  
int main()
{
    // object declaration
    B b;
    
    // pointer declaration
    int* p;
    
    // address of z is assigned to p
    p = &b.z;
    
    // initialization of z
    *p = 3;
    
    // points to previous location
    p--;
    
    // initialization of y
    *p = 4;
    
    // points to previous location
    p--;
    
    // initialization of x
    *p = 5;
    
    // passing address of x to function show()
    b.show(p);
  
    return 0;
}
输出:
x = 5 y = 4 z = 3