Re: Lahman, how ya doing?
- From: Andrew McDonagh <news@xxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 09 May 2005 00:17:09 +0100
Gregory L. Hansen wrote:
In article <d5iuj1$pl7$1@xxxxxxxxxxxxxxxxxxxxx>, Andrew McDonagh <news@xxxxxx> wrote:
Gregory L. Hansen wrote:
In article <0%Qde.43511$4v.31435@trndny03>, H. S. Lahman <hsl@xxxxxxxxxxxxxxxxx> wrote:
Responding to Hansen...
class EventElement { private: Object* recipient; int event_id; int tick_count; public: EventElement (Object* r, int e, int t) {recipient = r; event_id = e; tick_count = t;}; int getTickCount() {return tick_count;}; }
Let me ask you, why is this a class? I usually think of classes as something with member functions that do things with the data. I would have used a struct as a passive holder of data.
For one thing, it allows us to create instances which are immutable objects.
For another, we can use polymorphic calls with a class over a struct ('c' struct I take it you mean - as C++ structs are the same as Classes but with different default access rights).
Thanks, Andrew, but I'm not sure that meant anything to me.
I haven't been following this thread closely, but it appears you are using C++.
Immutable objects? Can't be changed?
Correct, can't be changed, but can be queried.
Is that better than a const struct?
I C++ a Struct and a Class are actually the same thing - choosing to use one over the other, simply implies what default visibility you want.
In the case of the Class code above, whilst the EventElement is created with three parameters, only tick_count is accessible to other objects, as its the only member var with a get() method.
In this case, the use of a Class (and actually the use of the 'private:' is ensuring encapsulation - all members are private.
So, is it better than a const struct? I'd say in this example yes, simply because we don't want to expose the other two member vars.
Your Mileage may vary.
I could use more words on the second paragraph, if you don't mind. Why is a polymorphic call with a class over a struct a good thing?
At the time I wasn't sure whether your 'struct' was something you'd used in 'c' rather than a C++ Struct which is different.
http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8
Therefore there is no difference with a polymorphic call on an instance of a Struct or a Class.
.
- Follow-Ups:
- Re: Lahman, how ya doing?
- From: Gregory L. Hansen
- Re: Lahman, how ya doing?
- References:
- Re: Lahman, how ya doing?
- From: H. S. Lahman
- Re: Lahman, how ya doing?
- From: Gregory L. Hansen
- Re: Lahman, how ya doing?
- From: Andrew McDonagh
- Re: Lahman, how ya doing?
- From: Gregory L. Hansen
- Re: Lahman, how ya doing?
- Prev by Date: Re: Help! Difficulty understanding DB -> Object mapping
- Next by Date: Re: Help! Difficulty understanding DB -> Object mapping
- Previous by thread: Re: Lahman, how ya doing?
- Next by thread: Re: Lahman, how ya doing?
- Index(es):
Relevant Pages
|