1

RAII 기능으로 인해 객체가 스택에만 배치 가능하고 객체 생성이 특수화 된 팩토리에 위임되어야하므로 ocpy 생성자가 액세스 할 수 없도록해야합니다. 용도.C++ 비 힙 보호 된 생성자 및 복사 생성자를 사용하여 팩토리 객체 생성

그래서 저는 이렇게했습니다.

template<typename Product, Args ... > 
class Creator : public Product 
{ 
    public: 
     static Product create(Args ... args) 
     { 
      return Product(args ...); 
     } 
}; 

class ProtectedClass 
{ 
     ProtectedClass(const ProtectedClass& aThat)=delete; 
     ProtectedClass& operator=(const ProtectedClass& aThat)=delete; 
    protected: 
     ProtectedClass(){} 
}; 

class Spawner 
{ 
    public: 
     ProtectedClass getProtectedClass() 
     { 
      return Creator<ProtectedClass>::create(); 
     } 
} 

int main() 
{ 
    Spawner spawner; 
    //I need protectedClass to be enclosed within this frame 
    ProtectedClass protectedClass = spawner.getProtectedClass(); // err copy constructor is delted 
} 

나는이

template<typename Product, Args ... > 
class Creator : public Product 
{ 
    public: 
     Creator(Args ... args) : product_(args ...){} 
     Product& get() const 
     { 
      return product_; 
     } 
    private: 
     Product product_; 
}; 

class Spawner 
{ 
    public: 
     std::unique_ptr<Creator<ProtectedClass>> getProtectedClassCreator() 
     { 
      return new Creator<ProtectedClass>(); 
     } 
} 

int main() 
{ 
    Spawner spawner; 
    std::unique_ptr<Creator<ProtectedClass>> creator = std::move(spawner.getProtectedClassCreator()); 
    ProtectedClass& protectedClass = creator->get(); 
} 

처럼 뭔가를 할 수 그러나 바로 보는 것 같습니다하지 않습니다.

다른 방법으로 어떤 문제를 해결할 수 있습니까?

답변

1

내가 할 수있는 방법은 복사본을 삭제하고 이동을 활성화하고 건설 키를 생성 할 수있는 모든 클래스를 통해 건설을 허용하는 것입니다.

// forward declare any factories 
class Spawner; 

struct ProtectedClass 
{ 
    class PermissionKey { 
     // this is a private constructor 
     PermissionKey() {}; 

     // make friends of the factories 
     friend Spawner; 
    }; 

    // all this is now public. 
    // because we have declared a constructor, the default constructor 
    // is deleted.  
    ProtectedClass(PermissionKey) {} 

    // disable copies 
    ProtectedClass(const ProtectedClass& aThat)=delete; 
    ProtectedClass& operator=(const ProtectedClass& aThat)=delete; 

    // enable moves so the factory can return it 
    ProtectedClass(ProtectedClass&& aThat)=default; 
    ProtectedClass& operator=(ProtectedClass&& aThat)=default; 
}; 

class Spawner 
{ 
public: 
    ProtectedClass getProtectedClass() 
    { 
     // construct our spawned object - we can create keys 
     return ProtectedClass(ProtectedClass::PermissionKey()); 
    } 
}; 

int main() 
{ 
    Spawner spawner; 
    //I need protectedClass to be enclosed within this frame 
    auto protectedClass = spawner.getProtectedClass(); // ok now 
} 
+0

결국 unique_ptr에갔습니다. dtors가 모든 이동 작업과 관련된 곳에서는 많은 문제가있었습니다. – user1079475

+0

@ user1079475 당신은 나보다 나은 유스 케이스를 알고 있습니다. 하지만 스택에있는 물건을 원한다고 생각 했나요? –

+0

그렇습니다.하지만 메소드 범위에서 호출 범위로 객체를 이동시키는 데 너무 많은 분주함이있었습니다. 이러한 객체에는 뮤텍스 속성이 있으며 힙에서 이러한 뮤텍스를 이동해야합니다. 작은 이득을 위해 끝에 너무 많은 문제가 있습니다. – user1079475