Should I pass std::string
by value or by reference to one function. This function store this values in member variable of class.
I am always confuse when about pass by value or reference. Please clear my confusion about this.
Here is code :
class DataStore {
public:
void addFile(const string& filename, const set< std::string>& filePaths)
{
if (dataStoreMap.insert(make_pair(filename, filePaths)).second)
{
cout << "Data Added" <<endl;
}
else
{
cout << "Data not Added" << endl;
}
}
private:
// member variable
map < string, set < string > > dataStoreMap;
};
Shall I make function declaration like this :
void addFile(const string& filename, const set< std::string>& filePaths)
or
void addFile(const string filename, const set< std::string> filePaths)
Both gives same result. If there any issue about memory allocation or performance.
Above function call from cpp
class.
DataStore ds;
set<string> setFileDirectory{ "1", "2", "3", "4", "6", "5" };
ds.addFile("file.txt", setFileDirectory);
setFileDirectory.erase(setFileDirectory.begin(), setFileDirectory.end());
setFileDirectory.insert({ "1", "2", "3", "4", "5", "6" });
ds.addFile("demo.txt", setFileDirectory);
Any detailed explanation would be appreciated.
Thanks