A philosophical question about const member function

N

neel

Hi,

I was wondering if there is any recommendation/guideline about similar
situation depicted in example given below:

class File {
bool CreateFile(const std::string & filename) {
CreateSpaceInFileSystemIfRequired();
// create file here..
}

bool CreateSpaceInFileSystemIfRequired() const {

SomeGlobalOrAnotherClassFunctionThatCreatesSpace();

}

};

C++ semantics dictate that as long as
"CreateSpaceInFileSystemIfRequired" does not change member of *File*
class, it should be okay to declare it as const. But, from point of
view of class functionality itself,
"CreateSpaceInFileSystemIfRequired" in a way cheats - without creating
space, it may not be possible to create file and although it's not
changing the file, from point of view of operation, it still is
affecting outcome of operation.

So in conjugation with operation or class functionality, I am of the
opinion that "CreateSpaceInFileSystemIfRequired" should be either non-
const or static member function. Is there any recommendation/
documentation regarding this scenario?

Thank in advance,
-Neel.
 
B

Bo Persson

neel said:
Hi,

I was wondering if there is any recommendation/guideline about
similar situation depicted in example given below:

class File {
bool CreateFile(const std::string & filename) {
CreateSpaceInFileSystemIfRequired();
// create file here..
}

bool CreateSpaceInFileSystemIfRequired() const {

SomeGlobalOrAnotherClassFunctionThatCreatesSpace();

}

};

C++ semantics dictate that as long as
"CreateSpaceInFileSystemIfRequired" does not change member of *File*
class, it should be okay to declare it as const. But, from point of
view of class functionality itself,
"CreateSpaceInFileSystemIfRequired" in a way cheats - without
creating space, it may not be possible to create file and although
it's not changing the file, from point of view of operation, it
still is affecting outcome of operation.

So in conjugation with operation or class functionality, I am of the
opinion that "CreateSpaceInFileSystemIfRequired" should be either
non- const or static member function. Is there any recommendation/
documentation regarding this scenario?

I think the fundamental question is why is it a member of File, if it
doesn't affects its state at all? Perhaps it should be a member of
some other class or in a separate namespace (FileSystemInterface?).


Bo Persson
 
P

Pavel

Bo said:
I think the fundamental question is why is it a member of File, if it
doesn't affects its state at all? Perhaps it should be a member of
some other class or in a separate namespace (FileSystemInterface?).


Bo Persson
I think it may be justified that CreateSpaceInFileSystemIfRequired() is
a class member because it probably uses the information encapsulated in
the class (the file name or maybe some derivative of it, validated,
transformed (maybe split to the directories) and maybe even enriched
with some additional information like file system, file attributes etc).

As for the OP question itself, I understand that "const" modifier could
be ok to use here. Technically "const" simply means the function will
receive 'this' pointer as a pointer to "const" object so that some
syntactic restrictions will be applied to what can be done withing this
function. As long as we do not mean to do any of that restricted stuff
to "this" pointee in this function, I think it is a good idea just to
let compiler notify us in the cheapest possible way (compile-time error)
if we did "it" by accident. If it is not by accident, it will never be
late to remove the specifier.

From the design perspective there is always temptation to assign more
"intelligence" or "semantics" to constness than it can ensure
technically. I try to resist this temptation as it is close in my mind
to making a single entity perform two unrelated functions without a good
reason -- something that has never played well for me.

-Pavel
 

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
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top