Boost logo

Boost Users :

Subject: [Boost-users] [flyweights] private constructor, friend access
From: r_at_[hidden]
Date: 2015-05-14 12:02:39


Dear all,

On a project of mine, I started to use Boost.Flyweight. I would like
users of my class to only be able to instantiate objects of my class by
using the Boost.Flyweight mechanism. For this, I make my class
constructor private. However, when doing so the corresponding flyweight
factory also cannot instantiate my class. What classes do I need to
`friend` in order to make this work?

Here's a small self-contained example of the problem:

class X {
public:
     int data() const { return m_data; }

     struct key_extractor
     {
         int operator()(const X& x) const { return x.data(); }
     }

     typedef boost::flyweights::flyweight
     <
         boost::flyweights::keyvalue
         <
             int,
             X,
             key_extractor
>
>
     flyweight;

private:
     int m_data;
     X(int x) : m_data(x) {}
     // friend declarations should go here
};

int main()
{
     auto my_x = X::flyweight(42);
     return 0;
}

Kind regards,

Raoul Wols


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net