嵌套类C ++,如何使外部方法成为嵌套类的朋友

时间:2015-05-22 16:21:10

标签: c++ class objective-c++

我需要从类stos中的方法“pop”中访问类信息中的私有数据。我知道我可以使用一种修改嵌套函数的特殊方法,但我认为使用“friend”不是elegnat。我想将外部方法作为嵌套类的朋友,但是我得到的信息“不能仅仅通过返回类型来重载函数”。有可能做到吗?

class stos
{
    class info
    {
        int x;
        bool isGood;
        friend info pop(); // warning: cannot overload functions distungished by return type alone
    };
    static const int SIZE = 10;
    int dane[SIZE];
    bool isEmpty;
    bool isFull;
    int *top;
public:
    stos();
    info pop();
    info push(int x);   
};

编辑:

   stos::info stos::pop()
{
    info objInfo;
    if (isEmpty == true)
    {
        objInfo.isGood = false;
        return objInfo;
    }

    objInfo.isGood = true;
    objInfo.x = *top;
    top--;
    return objInfo;

}

2 个答案:

答案 0 :(得分:3)

您可以在#include <iostream> using namespace std; class stos { class info { int x; bool isGood; friend class stos; //allow stos accessing private data of info info pop(){} }; static const int SIZE = 10; int dane[SIZE]; bool isEmpty; bool isFull; int *top; public: stos(); info pop(){ info a; a.pop(); //calling here a private method } info push(int x); }; int main() { // your code goes here return 0; } 的开头声明info类,然后再定义它。因此,您可以将类的定义更改为此

stos

这应该可以阻止错误。

答案 1 :(得分:1)

该代码编译正常,但您可能希望执行以下操作:

Delete from FundYearDetail where Fund_id In (2054,2056,2058,2059,2061,2063,2064,2065,2066,2067,2069,2072,2073,2076,2078,2079,2080,2081,2082,
2086,2088,2090,2093,2095,2096,2097,2099,2101,2102,2103,2104,2105,2106,2107,2109,2110,2114,2115,2116,2117,2118,2119,2342,2125,2126,2127,2128,2129,2130,2131)