访问私有数据类型

Accessing Private data types

本文关键字:数据类型 访问      更新时间:2023-10-16
#include <iostream>
using namespace std;
class A {
      private :
              typedef struct {
                      int a;
                      int j;
                      }type;
      public : 
             A(){};
            ~A(){};   
            void CreateInstance();               
        };
class B : public  A        
{
      private : 
              int d;
              int n;
      public :
             B(){};
            ~B(){};   
            void CreateInstance1();               
};

void A :: CreateInstance()
{ 
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}
void B :: CreateInstance1()
{ 
 // I want to create a Pointer/instance of structure in this function. Dont want to use Public method in Class A    
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}
int main()
{
 A obj;    
 obj.CreateInstance(); 
 B obj1;
 obj1.CreateInstance1();  
 cin.get();
 return 0;   
}        

我期待对此有一些建议。

  1. 如何在派生类中创建结构"类型"的实例。

请让我知道如何使用"数据类型"。

错误:"typedef struct A :: type

A :: type"是私有的。

提前谢谢。

你不能使用基类private的任何内容,这是语言的规则。

但是,您可以使用任何公共或受保护的内容。在您的情况下,调用基类函数可能就足够了CreateInstance

void B :: CreateInstance1()
{ 
    A::CreateInstance();
}

(一般来说,最好保持内聚命名:如果适用,请考虑将函数声明为虚拟CreateInstance,然后将CreateInstance1重命名为 CreateInstance 以使其覆盖A::CreateInstance。不过,这与问题无关)。

一些可能性:

  • 将类型更改为受保护(但您说不能)。

  • A中使用friend class B;(但我想你也可以使类型受到保护)。

  • 丑陋的黑客:在B中重新声明结构,创建相同的类型。然后,如果需要,请使用 memcpy 在类型的变量之间进行复制。

#include <iostream>
using namespace std;
class A {
      //woh that worked .. i did mistake here
      friend class B;
      private :
              typedef struct {
                      int a;
                      int j;
                      }type;
      public : 
             A(){};
            ~A(){};   
            void CreateInstance();               
        };
class B : public  A        
{
      private : 
              int d;
              int n;
      public :
             B(){};
            ~B(){};   
            void CreateInstance1();               
};

void A :: CreateInstance()
{ 
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}
void B :: CreateInstance1()
{ 
 // I want to create a Pointer/instance of structure in this function. Dont want to use Public method in Class A    
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}
int main()
{
 A obj;    
 obj.CreateInstance(); 
 B obj1;
 obj1.CreateInstance1();  
 cin.get();
 return 0;   
}        

如果你需要它工作得如此糟糕,你可以尝试这些行中的东西

在 A 中添加一个静态方法,用于创建对象实例

static A::type GetTypeInstance();

A::type A :: GetTypeInstance()
{
  A::type lc_Atype;
  return tp;
}

所以你的B::CreateInstance1将是

void B :: CreateInstance1()
{ 
 auto A(A::GetTypeInstance());
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

请注意,如果您的编译器不执行 RVO,它会创建不必要的临时对象

我的上一个答案使用 C++11。这是另一种在没有自动的情况下执行此操作的方法。

class A {
      private :
              typedef struct {
                      int a;
                      int j;
                      }type;
      public : 
             A(){};
            ~A(){};   
            void CreateInstance();   
            typedef type AType;    **//Added this typedef**
        };

将 B :: 创建实例 1 更改为

void B :: CreateInstance1()
{ 
 A::AType A;   **//Now this works**
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

此解决方案之所以有效,是因为 private 隐藏了名称,而不是类型本身。因此,我们仍然可以通过使用公共 typedef 包装类型来公开类型。