如何向朋友声明静态语言环境功能

时间:2016-04-20 15:32:59

标签: c++

我有以下代码:从接口派生的模板类需要使用静态本地函数。我宣布它是朋友;但是,该功能无法在此上下文中调用私人/受保护成员"。

如何正确设置此静态语言环境功能?

我指定我的类是模板,因为由于这个原因,我不能在其中使用内联友元函数(编译器说我的函数被重新定义)。

此外,我无法更改Interface类。

这是代码:

java.io.IOException: Async IO operation failed (1), reason: RC: 55  The specified network resource or device is no longer available.

    at com.ibm.io.async.AsyncLibrary$IOExceptionCache.<init>(AsyncLibrary.java:891)
    at com.ibm.io.async.AsyncLibrary$IOExceptionCache.get(AsyncLibrary.java:904)
    at com.ibm.io.async.AsyncLibrary.getIOException(AsyncLibrary.java:918)
    at com.ibm.io.async.AbstractAsyncChannel.multiIO(AbstractAsyncChannel.java:482)
    at com.ibm.io.async.AsyncSocketChannelHelper.read(AsyncSocketChannelHelper.java:217)
    at com.ibm.ws.tcp.channel.impl.AioSocketIOChannel.readAIOSync(AioSocketIOChannel.java:206)
    at com.ibm.ws.tcp.channel.impl.AioTCPReadRequestContextImpl.processSyncReadRequest(AioTCPReadRequestContextImpl.java:182)
    at com.ibm.ws.tcp.channel.impl.TCPReadRequestContextImpl.read(TCPReadRequestContextImpl.java:111)
    at com.ibm.ws.http.channel.outbound.impl.HttpOutboundServiceContextImpl.parseResponseMessageSync(HttpOutboundServiceContextImpl.java:1724)
    at com.ibm.ws.http.channel.outbound.impl.HttpOutboundServiceContextImpl.readSyncResponse(HttpOutboundServiceContextImpl.java:745)
    at com.ibm.ws.http.channel.outbound.impl.HttpOutboundServiceContextImpl.startResponseReadSync(HttpOutboundServiceContextImpl.java:1842)
    at com.ibm.ws.http.channel.outbound.impl.HttpOutboundServiceContextImpl.finishRequestMessage(HttpOutboundServiceContextImpl.java:1245)
    at com.ibm.ws.websvcs.transport.http.out.HttpOutSyncWriter.finishBufferRequest(HttpOutSyncWriter.java:94)
    at com.ibm.ws.websvcs.transport.http.out.HttpOutWriter.writeBuffer(HttpOutWriter.java:136)
    at com.ibm.ws.websvcs.transport.http.out.HttpOutByteBufferOutputStream.finish(HttpOutByteBufferOutputStream.java:468)
    at com.ibm.ws.websvcs.transport.http.SOAPOverHTTPSender.sendChunkedRequest(SOAPOverHTTPSender.java:882)
    at com.ibm.ws.websvcs.transport.http.SOAPOverHTTPSender.sendSOAPRequest(SOAPOverHTTPSender.java:799)
    at com.ibm.ws.websvcs.transport.http.SOAPOverHTTPSender.send(SOAPOverHTTPSender.java:578)
    at com.ibm.ws.websvcs.transport.http.HTTPTransportSender.invoke(HTTPTransportSender.java:366)

3 个答案:

答案 0 :(得分:3)

barFoo的朋友,但不是Interface的朋友。并且无需将bar声明为Foo的朋友,因为它仅在Interface上调用受保护的成员函数。您可以将其声明为Interface的朋友。

class Interface;
static void bar(Interface *b);

class Interface
{
    friend void bar(Interface *b);
    protected:
        virtual void virt(void) = 0;
};

答案 1 :(得分:1)

对于

static void bar(Interface *b)
{
    b->virt(); // Error: virt() is protected.
}

为了工作,您必须bar friend Interface。它是否是派生类的friend也不会产生影响。

答案 2 :(得分:1)

如果您无法更改Interface类,那么您只需引入一个中间类即可转到受保护的成员:

class Interface {
protected:
    virtual void virt(void) = 0;
};

class Interface2 : public Interface {
public:
    virtual void virt(void) = 0;

};

static void bar(Interface2 *b);

template<class T>
class Foo : public Interface2 {
    // How to properly say the local function bar is friend ?
    friend void bar(Interface2 *b);

    T val;

public:
    void virt(void)
    {
    }

    void func(void)
    {
        bar(this);
    }
};

static void bar(Interface2 *b)
{
    b->virt(); // Error: virt() is protected.
}

int     main(void)
{
    Foo<int>    foo;

    foo.func();
    return 0;
}