Hi all, thanks for your comments on this. Practically speaking, in my program I could easily get over this since the beginning, I am actually using a sort of user validation right when the information are inserted, so there is not the possibility the field is empty (it might be wrong but not empty) and then I simply that value to the member before calling the function (here I am using some setters instead of pubic members)
1 2 3 4 5 6 7 8 9
|
remote = new SyncHandler;
remote->setRemoteHost(settings.value("remoteHost").toString());
remote->setSourceDir(settings.value("srcDir").toString());
remote->setDestDir(settings.value("destDir").toString());
// verify if remote host is available (ping)
if ( remote->verifyConnection() != 0 ) {
// throw an error and exit
|
So far so good, I could actually cope with this. But question came in mind when I started to think about abstraction. I will never happen but what if anyone else wants to use my class for his code? He does not care about the logic in my class, it just has to work, however in my code there are these conditions of dependencies which need to be satisfied (call f() only after defining X member) otherwise it simply won't work.
Is it common in C++? What are the suggestion? Simply trying to avoid this dependencies when possible? What when it is not possible at all?
The solution of settings a common or fake value like the loopback interface came in my mind as well but it is good I think.
Instead, I seem to understand this is the right way to go for:
Just as you would with a loop that ends on some condition, you will set X and then and only then call the function, does that make sense?
|
but unfortunately I do no understand how in my case.
Also I see that I could just put a check in the function body, like iHutch105 said, and I think this is pretty much easy to get. I could simply say:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// Functions
int MyClass::MyFunction()
{
if (x == 0) // it has not been initialized yet
{
return -1;
}
else
{
// Do stuff here
return 0;
}
}
|
and then for example simply document that MyFunction() requires X to be initialized else it will return a value -1. Does it work?
So I am really just trying to understand what is rule to follow in this case and what must be avoided in order to not break this rule.