I want to overload the std::string copy constructor for a reference type. Basically, I am trying to check whether std::move() will invoke the copy constructor or not. I am calling func() using reference. As I understand reference passing is nothing but just a pointer that will be passed there. In this function, I am calling func(t) i.e. t by reference. I have two questions, how can I overload std::string copy constructor here so that it calls my copy constructor of std::string of reference copying rather than std::string. Next, if I can achieve that how can I stop the call of copy constructor using std::move(). My understanding is that once I will call func(t), let's say be value then a copy constructor will be invoked to create that temporary new object. Let's say if I pass by reference in my case here then a temporary variable having the reference i.e. memory address will be created? If yes, then I want to overload that reference copy constructor and see whether it is true or not? Next, if it is true is it true to say that std::move() will avoid the call to reference copy constructor?
#include <iostream>
#include <string>
using namespace std;
string& string(const string &other)
{
cout<<"my copy constructor invoked"<<endl;
}
void func(const std::string &val)
{
cout<<"func invoked"<<endl;
}
int main()
{
std::string t = "hello";
func(t);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire