[sword-devel] C++ design question
Scott Davis
sword-devel@crosswire.org
Fri, 21 Jan 2000 16:55:53 -0500 (EST)
I suppose this is a nice chance to introduce myself. I have been lurking
on the list for a few weeks. I was planning to get involved with this
project, but the demands of this semester have turned out to be too
difficult. I am currently a senior in Computer Engineering at Virginia
Tech. In addition to school, I work about 20 hours a week programming and
lead a homegroup that meets at my place for worship, prayer, teaching and
fellowship.
I have been programming in C++ for many years, but the one rule I've
learned is that everyone likes to code differently. So these are simply
my personal coding opinions:
1) returning by copy CData getText()
This is the simplest method, but will invoke a large overhead if the data
structure is large. Also dangerous if the data structure has pointers to
data - you will need to create copy constructors that dynamically create
and copy each data element.
2) returning by pointer CData *getText()
gets away from the problems of returning by copy, but introduces its own
problems. If this is actually CData* SomeClass::getText() and SomeClass
is simply returning a pointer to internal data that it takes care of, that
will work well. But declare it as const CData *getText() so that you can
modify or delete SomeClass's internal data. If this is not the case, and
getText would have to dynamically create the CData object and then return
it, you are asking for trouble. Not a problem as long as you **DOCUMENT
STRONGLY**, but you will need to make sure that the CData objects are
deleted by the calling code.
3) using a reference.
Probably the best way to do it in htis case, if I understand the problem
correctly. Create an empty CData object and pass it to the getText()
method. getText fills the object and returns it. Safe. Easy. No mess.
Just my 2 cents, of course.
The reason you haven't found a good answer is that there probably isn't a
perfect answer. Each has its advantages and limitations. I would
definitely stay away from return by copy, though, with a large data
structure.
Hope this is helpful.
--- Scott Davis
On 21 Jan 2000, Torsten Uhlmann wrote:
> I want to bother you with some boring C++ stuff :)
>
> You know we currently totally redesign BibleTime in order to first
> support MDI, KDE2 and secondly to get a much more flexible backend. The
> design will allow us to view text files, audios etc. as modules and have
> them along with the SWORD modules. Anyway, simple question yet I can't
> find a good answer.
>
> I have created a class CData which will hold the result of a query to a
> module, module type, data type and used key, so it can hold qiet some
> data. I don't know what is the proper way of returning such an
> object. Possibilities are:
>
> CData getText(key) or
> getText(key, CData&) or
> CData *getText(key)
>
> In the last one the caller has to destroy the CData object itself.
> I want to do it right this time so I would like you to give me advise!
>
> Thanks for your response...
>
>
> --
>
> best regards,
> Torsten Uhlmann
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> TUhlmann@gmx.de
> TUhlmann@debis.com
>
> http://www.tuhlmann.purespace.de
> http://www.bibletime.de
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Wise men still seek him!
>