What is the correct way of using or creating a move constructor?
Here is an example:
class Example
{
public:
Example( int && p_Number,
bool && p_YesNo,
std::string && p_String ) : // Error here
Number( p_Number ),
YesNo( p_YesNo ),
String( p_String )
{
};
private:
int Number;
bool YesNo;
std::string String;
std::vector< unsigned char > Vector;
};
void ShowExample( void )
{
Example ExampleA( 2013,
true,
"HelloWorld" // Why won't it work?
);
};
I've shown the errors in the comment.
Edit: *Okay, I am now sure what I have is not a move constructor. So, can I write one?*
"HelloWorld"isconst. Are you accounting for this?"HelloWorld" // Why won't it work?If you're using VC++ 2010, it's because of a bug (fixed in VC++ 2012).String(std::move(p_String))etc. But what you have is not the "move constructor". I'd rather call "a moving constructor".className(className&&)just like there is only one copy constructor per class:className(const className&)but you still can have constructors that accept rvalues (ie. move semantics) on specific arguments. It's just a name issue.String(std::move(p_String))to move it, becausep_Stringis not an rvalue