General Beginner Question

M

magnus.moraberg

Hi,

Is it possible to combine the following into one function -

inline virtual void saveDataToFile()
{
if(isInitialized)
saveDataToFile( this->filePath);
else
throw MyException ("Can't save this object to file since it has not
been initialized from such a file");
}

inline virtual void saveDataToFile(const string& filePath)
{
if(isInitialized)
saveDataToFile( filePath);
else
throw MyException ("Can't save this object to file since it has not
been initialized from such a file");
}

Thanks,

Barry
 
Z

Zeppe

Hi,

Is it possible to combine the following into one function -

inline virtual void saveDataToFile() [...]

inline virtual void saveDataToFile(const string& filePath)
[...]

unfortunately default arguments cannot take non-static class member
values, so you have two choices:

1) use a default empty filename (the "special value" approach):
inline virtual void saveDataToFile(const string& filePath = ""){
if(filePath.empty())
filePath = this->filePath;
// ...
}


2) have two function but one implementation (I'd prefer this, much neater)
inline virtual void saveDataToFile() { saveDataToFile(this->filePath); }
inline virtual void saveDataToFile(const string& filePath){
// as before
}

Best wishes,

Zeppe
 
M

Michael DOUBEZ

Hi,

Is it possible to combine the following into one function -

inline virtual void saveDataToFile()
{
if(isInitialized)
saveDataToFile( this->filePath);
else
throw MyException ("Can't save this object to file since it has not
been initialized from such a file");
}

inline virtual void saveDataToFile(const string& filePath)
{
if(isInitialized)
saveDataToFile( filePath);
else
throw MyException ("Can't save this object to file since it has not
been initialized from such a file");
}

virtual void saveDataToFile(const string& filePath=string())
{
if(!isInitialized)
{
throw MyException("Can't save this object to file since it has not
been initialized from such a file");
}

if(filePath.empty())saveDataToFile(this->filePath);
else saveDataToFile(filePath);
}
 
J

James Kanze

(e-mail address removed) wrote:
Is it possible to combine the following into one function -
inline virtual void saveDataToFile() [...]
inline virtual void saveDataToFile(const string& filePath) [...]

unfortunately default arguments cannot take non-static class
member values, so you have two choices:
1) use a default empty filename (the "special value" approach):
inline virtual void saveDataToFile(const string& filePath = ""){
if(filePath.empty())
filePath = this->filePath;
// ...

I'd suggest using pass by value if want to do this. Or
something like:

std::string const* path
= (filePath.empty() ? this->filePath : filePath) ;

in the code (and replacing use of filePath with *path, of
course).
2) have two function but one implementation (I'd prefer this, much neater)
inline virtual void saveDataToFile() { saveDataToFile(this->filePath); }
inline virtual void saveDataToFile(const string& filePath){
// as before
}

I prefer the second solution as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top