Re: C++ and Linked Lists Baggett/DOOMer at "Mar 15, 98 12:28:30 pm"

From: Shadowdancer (sg618lo@UNIDUI.UNI-DUISBURG.DE)
Date: 03/15/98


> At 01:52 PM 3/15/98 +0100, you wrote:
> >Greetings,
> >
> >  You might consider using the container classes of the Standard
> >Template Library? You'd then declare it like this:
> >
> >#include <vector>
> >
> >vector<clan_info> clan_array;
> >
> >You can add elements by:
> >clan_array.push_back(some-clan-info);
> >
> >You can get the array size by:
> >clan_array.size()
> >
> >and adress elements with the operator[]
> >clan_array[x].somefield
> >
> >I use the STL in my code pretty much and am pretty content with it.
> >Just a word of warning though: don't put pointers in STL containers,
> >but just real instances.
> >
> >- Chris
>
> Thanks for the insight, but if i can't put pointers in it, what's the
> point? :-)
> I need to store pointers to strings in it, along with other info
> and I was wondering if it was possible to do something like that.

Oh, I meant, don't use pointers as elements of the stl container, like:
vector<struct clan_info *> clan_info_array;
but rather:
vector<struct clan_info> clan_info_array;

the struct itself can of course have pointers :)
this is because if you have a pointer in the container, it can not
guarantee that the memory it points to is still valid, i.e. the
object there still exists.

You can then work with references! Instead of e.g. having:
void foo (struct clan_info *bar) {
  bar->something = something_else;
};

call could be like:
foo(&clan_info_array[0]);

you can have:
void foo (struct clan_info &bar) {
  bar.something = something_else;
};

call could be like:
foo (clan_info_array[0]);

note that bar in the second case is actually treated like a real
instance and not like a pointer, its just another name for the
real instance, that is e.g. in the vector-container.

>
> And I'm just wondering, but it IS possible to access private members
> of a class if you declare the operator a friend (or is it virtual friend?)
> am I correct?

Yes, it is possible, but I prefer to have good encapsulation and as
few friends as possible (I use a friend function in a class, which
overrides the operator<< for cat'ing a buffer, and a function called
'sendit' to release it. sendit is friend). You should rather write
member functions that do what you want to have done with the datafields.

Disadvantages:
  - function call overhead (depending on complexity can be declared
    'inline' though!)

Advantages:
  - if something goes awry, you just have one place to look for the bug:
    the member function, and not at a billion places in the code
    It was a horror when we had a buffer overflow at a datafield that
    was written to in quite a few places in the code...but it disappeared
    when we finally had it encapsulated pretty neatly :)

>
> this makes it possible to have a string class, and do something like this
> string f, b;
> char c[80];
>
> f = b + c;
> or
> f = c + b;
> if you have the the operator + (with a char sent to it) set as a friend of the
> class.
>
> I'm hoping i'm right, i thought I had read that somewhere.
>

the string class, as defined in:
#include <string>

has the operator+= overriden for that purpose:
b = "something";
f = b;
f += c;

because I'm a fan of the stream semantic, and because multiple
+=s seem awkward to me, I prefer a wrapper class for the string
class, pretty easily written, that overrides << for everything
and thus a string catentation could look like this:

class StringWrapper str_wrap;

str_wrap << b << c;

> Code On
> Akuma the Raging Coder
>

Hope to have helped,
- Chris


--
/----------------------------------------------------------------------------\
| Christian Loth                      | Meet me as the >Shadowdancer< in the |
| chris@rom.mud.de                    |           REALM OF MAGIC             |
| http://rom.mud.de/~chris            | telnet rom.mud.de 4000               |
|        * NEW *                      | http://rom.mud.de/                   |
|               "The road goes ever on and on!" - Bilbo Baggins              |
\----------------------------------------------------------------------------/


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST