I'd like to have a shared_ptr type object in a class, and one of the class' member functions should assign who the shared_ptr points to.
In code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class A
{
public:
std::shared_ptr<some_type> Shared_Pointer;
void Assign_Pointer();
};
void A::Assign_Pointer()
{
some_type Some_Type;
//how do I "point" the shared_ptr to Some_Type here?
}
I knew how to do this before using plain old pointers and new; the problem is that the more I read up on it the more I realize that there is never a reason to NOT use a shared_ptr. Any idea of what to do?
Thanks, I must have actually been thinking of unique_ptr's when I posted.
The reason I do not assign the pointer when A is constructed is that in my program Some_Type is a file stream. When the file stream is called, it has to be initialized with a string for the location of the file. Thus, the issue is that when A is initialized, the file location is not known. A has a member function that asks the user to input the name of the file. It seems that (given the way I've designed my program), it is not possible to get away without using a pointer of some sort, and it's not possible to assign the value of the pointer in the class constructor.
Thank you for the reply. I will look into MoveConstructible and MoveAssignable properties. I didn't mention this in the previous post, but the stream (and file) are actually QT types: QTextStream and QFile. If those also have the property you mentioned I will look into exploiting that. Thank you!