Re: Class function help
On Dec 1, 12:04 am, JackC <jeche...@gmail.com> wrote:
On 30 Nov, 22:42, "Alf P. Steinbach" <al...@start.no> wrote:
Thanks for the help, I suppose the best way would be to return
a pointer to it, then i can return a null pointer if it is not
That's what I'd do. I think, at least; I'd have to have more
context to be sure.
When you say did i mean to pass the alias string by value,
well i did because i have never really been taught any better.
Is this just considered bad programming in terms of using
unnecessary resources when i could use a pointer instead?
It's not really wrong, but most people have the automatique
habit of passing class types by const reference.
And as for the style, again its what i picked up from when i
started programming when I was about 13, maybe i should try
and get out of the habit though.
You say using Get is bad, I have created Get/Set functions and
kept the variable public, is this again a wrong approach?
Again, it depends. Usually, you don't want public or protected
data members (although there are exceptions), and I'm pretty
sure that Alf isn't recommending that. If the "value" is a
simple attribute of the object, then it is conceptually a
"variable"; the function name reflects this, and is a qualified
noun (e.g. status)---it is often overloaded with a const member
which returns the value, and a non-const member which takes a
new value as an argument. (Note, however, that too much of this
is generally a sign that you haven't really understood
encapsulation. Most "attributes" of a class probably shouldn't
be visible as attributes from the outside. But again, there are
In the case in question, however, I'm not sure that we are
dealing with an attribute. If the function is an explicit
look-up, then using get as part of the name is fine. Some
people also recommend it for attributes. I don't, but that is a
style issue. If the function represents some action, however,
and not just state, then the name should be a verb.
Of course, as verbs go, "get" is not particularly precise.
Depending on the context, perhaps "find" or "create" would be
better. (In this case, "find", of course, rather than
"create".) Or something application specific, even more
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34