Re: string class question..

From: Justin Dubs (jtdubs_at_eos.ncsu.edu)
Date: 12/10/03


Date: 9 Dec 2003 22:24:47 -0800


"Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message news:<lmsBb.351$JR3.140@twister.nyroc.rr.com>...
> "Justin Dubs" <jtdubs@eos.ncsu.edu> wrote in message
> news:2e262238.0312090007.514446ca@posting.google.com...
> > "Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
> news:<opbBb.179602$ji3.22140@twister.nyroc.rr.com>...
> > > "JustSomeGuy" <nope@nottelling.com> wrote in message
> > > news:br3d5b$111s$1@nserve1.acs.ucalgary.ca...
> > > > 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.
 
> > with private data members and two
> > constructors and getters when a plain struct would work just fine.
>
> 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.

> > Not to mention that you didn't provide setters and your only getter
> > uses const references so your class doesn't even provide the same
> > functionality as his struct because you can't modify the a and b in
> > your version.
>
> 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.
 
> >
> > struct atype {
> > std::string a;
> > std::string b;
> >
> > atype(const std::string &a, const std::string &b) : a(a), b(b) { }
> > };
> >
> > Depending on the usage, this little struct may be just perfect.
>
> Yes, maybe.

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.

> > There's no reason to make it a class with private data members unless
> > encapsulation or data hiding is an issue.
>
> They are always issues.

God, I wish you were joking. Again I will bring out the hammer/nail
analogy.

> > And god help you if you
> > make them private AND then include getters and setters for them.
>
> Huh?

LOL

> > > or better yet
> > >
> > > #include <vector>
> > >
> > > std::vector<atype> avec;
> > > avec.push_back(atype("ABC", "123"));
> > > avec.push_back(atype("DEF", "456"));
> >
> > Why is this "better yet?" Yeah, the vector is resizeable. It also
> > has overhead both in memory and speed.
>
> 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.
 
> > What if he didn't need to
> > resize it. Is your way still better then?
>
> Probably.

That's a shame.

> > There's a time and a place for super-dooper OO patterns, but this may
> > not necessarily be it.
>
> 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



Relevant Pages

  • Re: reading from a serial port
    ... >> array of structs, it uses a pointer to one element in an array ... >far enough to know that FILE is possibly a typedef to some struct. ... Ritchie, "The C Programming Language", 2nd Edition. ... the stdio library allocates a data buffer (BUFSIZ in stdio.h is ...
    (comp.unix.programmer)
  • Re: WinCE Struct Help
    ... Some of us just started programming while others are more advanced. ... >> what do you mean basic concept of malloc and realloc?? ... But I want to use struct. ... >> malloc or realloc function for struct? ...
    (microsoft.public.windowsce.app.development)
  • Re: Getting class pointer from interface pointer [dont use structures?]
    ... reading a book on MFC programming by Richard M. Jones "Intro to MFC ... Jones is a professional and teaches as well, ... Say I have this struct definition: ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Object-oriented programming in standard ANSI C
    ... The C computer programming language allows you to encapsulate data ... There have been many discussions whether encapsulation and information ... (publishing a struct declaration without publishing it's definition) ...
    (comp.lang.c)
  • Re: Object orientation.....
    ... meant by "object based" programming which has some of the notions of OO ... (e.g., data hiding) ... ajm. ... Prev by Date: ...
    (comp.lang.c)