Cy Edmunds said:
Justin Dubs said:
"Cy Edmunds" <
[email protected]> wrote in message
I have a structure
typedef struct
{
string a;
string b;
} atype;
Assuming "string" means std::string this is valid C++. But it sure looks
like C. If you want to package two strings together it might be better to
use a class in C++.
Why is a class better? There is no difference between a struct and a
class in C++ aside from the default access specifier being private for
classes and public for structs.
I don't know why this code works with any compiler. A C++ solution would
look like this:
[ snip the monstrosity ]
Why the heck do you think that's a better way to put to strings
together? All that overhead
"Overhead" is a term I hear a lot. Show me your timing or memory usage
studies and I will take due note. However, maintainability is often more
important than raw performance, so faster and/or smaller isn't necessarily
better anyway.
I didn't necessarily mean timing or memory overhead. I may have meant
person-time overhead. The amount of time I waste typing all that crap
when all I needed was a struct.
I suspect you do not understand the basics of object oriented programming if
you think public data is OK.
I recommend you do some more reading on OO theory before you say such
things. The definition of OO itself is hotly debated. Many of the
acceptable definitions to not even include data hiding. Even the ones
that do usually do not tout it as the be-all end-all of programming.
Like all programming techniques, data hiding has it's uses. However,
it is not the solution to all problems. You are a hammer, but not all
problems are nails.
Yes, the structure has more "functionality" that anybody is free to
modify the contents directly. This is of course a disadvantage.
This is trivially false. It CAN be a disadvantage, in some programs,
some of the time. Not in all programs, all of the time. This is
especially true of throw-away programs that are designed to conserve
programmer-time as there are likely no future maintainability issues.
Now you contradict yourself. A few sentences ago you said that public
data members are always bad. In your next sentence you will state
that data hiding is always an issue. Yet here you state that this
struct may be acceptable.
God, I wish you were joking. Again I will bring out the hammer/nail
analogy.
LOL
Ah yes, once again optimizing for performance without any testing or
consideration of requirements.
What requirements are those? I don't recall the OP stating that this
code was for a production system that would definitly need future
expantion capabilities. It may just be a throw-away. Either way,
vector's aren't always the answer and it is naive to say otherwise.
That's a shame.
Keep in mind that this was posted in comp.lang.C++. I have no issue with
people who prefer to program in C. But if you want to program in C++ I think
you should do it right.
C++. You keep using that word. I do not think it means what you
think it means.
C++ is a multi-paradigm language. Yes, it does support OO, for
various definitions of OO. It also supports procedural programming.
It also supports label/goto-based spaghetti code. None of these are
"right" or "wrong." To say that using data hiding and classes is the
only "right" way to do C++ is rather sad.
Justin Dubs