访问私有数据类型
Accessing Private data types
#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;
}
我期待对此有一些建议。
- 如何在派生类中创建结构"类型"的实例。
请让我知道如何使用"数据类型"。
错误:"typedef struct A :: typeA :: 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 包装类型来公开类型。
相关文章:
- 防止主数据类型C++的隐式转换
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在C++中打印指向不同基元数据类型的指针的内存地址
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何计算数据类型的范围,例如int
- C++中数据类型修饰符的顺序
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 特定数据类型的模板类
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 有没有办法提示用户使用哪种数据类型作为模板 c++
- 如何访问从 COM 对象返回的 VARIANT 数据类型中的安全数组C++?
- 是否应该对复杂数据类型使用访问器
- 如果只存在私有函数,则访问基类中的私有数据类型
- 访问私有数据类型
- C++:从只能通过迭代器访问的数据类型中高效地提取元素子集
- C /c++数据类型,通过指针实现最快的可移植访问
- 访问模板中的变量,该模板是具有多个变量的自定义数据类型的向量
- 为什么在访问矩阵中的像素时使用不同的数据类型会得到不同的值?
- 如何将在一个类中声明的类的数据类型访问到另一个类(两者都在不同的翻译单元中)
- 如何访问结构中的int指针数据类型并为其赋值