c++ - 在派生类中可见的私有(private) typedef

标签 c++ inheritance typedef

我的编译器 (VC++ 6.0) 有一个小问题。在我看来,这样的代码应该会导致错误;

class Base
{
    private:
        typedef int T;
};

class Derived : private Base // Here the Base class can be inherited publicly as well. It does not play any role
{
    public:
        T z;
};



int main()
{
    Derived obj;
    obj.z = 7;
    return 0;
}

这段代码在VC++6.0下编译运行没有任何问题。

关于软件设计,这段代码并不完美。不应将类成员中的任何一个成员声明为公共(public)的。但是我对这方面不感兴趣。

我的问题是 typedef。 typedef 在基类中声明为私有(private)。从我对 C++ 的理解来看,这个 typedef 必须对派生类或 main() 函数不可见。但两者都完美地看待它们。

有人对这种现象有解释吗?

提前致谢

尼吉普

最佳答案

此行为在 VC++6.0 中是不一致的,您应该在定义 Derived::z 时遇到错误。 (除非您有商业原因需要使用它,否则还有其他选择在技术上比 的 VC++6.0 更可取)。

关于c++ - 在派生类中可见的私有(private) typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1356204/

相关文章:

c++ - 将 'typedef' 从基类传播到 'template' 的派生类

python - 从 C++ 调用 Python 或 Lua 来计算表达式,仅在需要时计算未知变量

c++ - Visual Studio C++ 代码错误。 Lab4.obj : error LNK2019: unresolved external symbol "bool __cdecl

c++ - cpp中的类实例化

c - 什么是 (void (**) ()) 以及如何对它进行类型定义?

c - 将 typedef 结构作为参数传递给函数

c++ - 非静态成员 C++98 的无效使用

c++ - 在 Armadillo C++ 中为矩阵类型变量获取键盘输入

C# 继承构造函数子和父 ??

c++ - 当保护保护太多