I have a Visual Studio 2008 C++ program where I have my own stream implementation. Something like this:
class Foo : public std::ostream
{
public:
Foo( int a ) : std::ostream( &buf_ ) { };
Foo( boost::shared_ptr< int > a ) : std::ostream( &buf_ ) { };
private:
std::filebuf buf_;
};
class Bar
{
public:
Foo GetFoo() { return Foo( 1 ); };
Foo GetFoo2() { return Foo( boost::shared_ptr< int >( new int( 1 ) ) ); };
};
The intended usage is like this:
Bar b;
Foo f = b.GetFoo(); // works fine
Foo f2 = b.GetFoo2(); // compiler error
Unfortunately, this gives me a compiler error about the basic_ios copy constructor.
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
Why is the first Foo( int ) constructor okay with the compiler, but the one involving a boost::shared_ptr is not?
Thanks,
PaulH
Your
Fooclass is inheriting from a non-copyable class, yet you are not implementing the copy constructor. You are bound to get into trouble when you pass instances ofFooby value, as you do in the getter functions.The reason that
GetFooworks might be that return-value optimization is eliding the copy construction, but this doesn’t mean that you’re allowed to do this.