close

#include <iostream>
#include <memory>

using namespace std ;

class Foo1 ;
class PFoo1 {
private:
    shared_ptr<Foo1> foo;
public:
    PFoo1();
    PFoo1(const PFoo1& pf);
    PFoo1& operator=(const PFoo1& pf);

    void DoSomething();
    void DoSomethingElse();
};
class Foo1 {
friend class PFoo1;
protected:
 Foo1()=default;
public:
 void DoSomething();
 void DoSomethingElse();
};

PFoo1::PFoo1()
{
    foo = make_shared<Foo1>();
}

PFoo1::PFoo1(const PFoo1& pf)
{
    foo = make_shared<Foo1>(*(pf.foo)) ;
}

PFoo1& PFoo1::operator=(const PFoo1& pf)
{
    foo = make_shared<Foo1>(*(pf.foo)) ;
    return *this;
}

void PFoo1::DoSomething()
{
    foo->DoSomething();
}

void PFoo1::DoSomethingElse()
{
    foo->DoSomethingElse();
}

void Foo1::DoSomething()
{
 cout << "Foo::DoSomething()" << endl;
}

void Foo1::DoSomethingElse()
{
 cout << "Foo::DoSomethingElse()" << endl;
}

int main()
{
    PFoo1 f ;
    f.DoSomething() ;
    f.DoSomethingElse() ;
}

 

[Pattern]$ ./PassKey2.exe
Foo::DoSomething()
Foo::DoSomethingElse()

http://stackoverflow.com/questions/3217390/clean-c-granular-friend-equivalent-answer-attorney-client-idiom/3217430#3217430

 

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()