代码之家  ›  专栏  ›  技术社区  ›  Priyanka Mishra

访问私有类中的公共类函数时出错

c++
  •  0
  • Priyanka Mishra  · 技术社区  · 15 年前
    class test
    {
    private:
        class privateStruct
        {
        public:
            int m;
            privateStruct(int p){m=p;}
        };
    };
    
    void ff()
    {
        test::privateStruct ps(4);
        throw ps; //Does not work.. 
    }
    
    void main()
    {
        try
        {
            ff();
        }
        catch(...)
        {
        }
    }
    

    但是下面的代码可以工作为什么

    class test
    {
    private:
        class privateStruct
        {
        public:
            int m;
            privateStruct(int p){m=p;}
        };
    };
    
    void ff()
    {
        throw test::privateStruct(4); //Work why
    }
    
    void main()
    {
        try
        {
            ff();
        }
        catch(...)
        {
        }
    }
    

    我需要答案为什么上面的代码工作。

    3 回复  |  直到 15 年前
        1
  •  5
  •   Collin Dauphinee    15 年前

    这是visualstudio6.0中的一个旧的/已知的bug。它在构造临时表时忽略访问说明符。没有可用的修复程序。

    将警告级别提高到3或更高(/W3)将导致有问题的代码发出警告。

        2
  •  1
  •   Drew Dormann    15 年前

    第二个例子中的代码之所以工作是因为Visual C++ 6因其标准符合标准而臭名昭著。

    它的工作是偶然的。

        3
  •  0
  •   Sameer    15 年前

    即使是第二个代码段也无法编译。无法在函数ff()中访问privateStruct。