Slimelite BBS

Re: Good Luck!?

Back to R25C

From Ant Moore · 1996-04-07 18:15 · #75233 in R25C

In reply to Good Luck!? by Bill Birrell

 -=> Quoting Pat Winstanley to Ant Moore <=-

 PW> Hi Ant,
Watcher Pat!

 PW> Thursday March 28 1996 19:27, Ant Moore wrote to Barry Pearce:
 
 AM> Personally, ATM I use C++ language extensions where
 AM> appropriate, within otherwise pretty normal(?) C code. I
 AM> quite like the idea of having a constructor to set any
 AM> structures (not classes) I use to pre-determined values.

 PW> Sounds interesting. How do you go about that, roughly? I'm easing
 PW> myself into C++ bit by bit simply by using some of the extensions when
 PW> they seem appropriate, and that sounds useful.

======================
OK, here goes:


struct MYSTRUCT
{
     char buff[50];                 
     int  index;

     MYSTRUCT :: MYSTRUCT (void)     // Constructor for this structure
     {
          index = 0;
          strcpy (buff, "INIT");
     }
};

If in your code, you declare a structure of this type:

void main (void)
{
MYSTRUCT a, b;                        
     //  Now do something with them!
}

When the structures a and b are created, the constructor is run for each of
them. So both are initialised. Constructors can accept parameters too, but
they don't return anything. For instance:

struct MYSTRUCT
{
     char *buff;                 
     int  index;

     MYSTRUCT :: MYSTRUCT (char *initstr)    // Constructor for this structure
     {
          index = strlen (initstr);
          buff = new char (index + 1);    
          strcpy (buff, str);
     }

     MYSTRUCT :: ~MYSTRUCT (void)            // Destructor for MYSTRUC
     {
          free (buff);
     }
};

If in your code, you declare a structure of this type:

void main (void)
{
MYSTRUCT a ("Struct A"),
         b ("Structure B");                        

     //  Now do something with them!
}

In this case, both the structures are created with the string stored, and
the index element initialised to the string length. In this case, the
constructor allocates memory to store the string in (the "new" command is
like malloc, only much better!), so when the structure goes out of scope
it needs cleaning up. You can't just lose the structure, you need to lose
the memory allocated to it as well. This is what the destructor does. Notew
there is no call to it: it's called automatically when the structure goes
out of scope.

When you come to create a linked list of structures, this "initialisation
at creation" is extremely useful, as is the ability to create new structures
on the fly:

void main (void)
{
MYSTRUCT *msptr1, msptr2;

     msptr1 = new MYSTRUCT ("Init String");
     msptr2 = mew MYSTRUCT ("Another Init String");
}
Sorry if that was all a bit wordy.   :-)

And not a Booch, Yourdon, Schloer-Mellor in sight! (Sorry Barry!)

===========================

[Cut]

I'm sure that there are plenty of Pearces oops I mean people in this echo
who can help you out in detail with C++, if and when you choose to study
it in depth.

Cheers!

Ant. (Compu$erve 100556,2571)

... There's nothing quite like a McDonalds. Dog-S**t comes close though
--- LoraBBS-OS/2 v2.40b2+
 * Origin: The Rowley Rag (2:2500/76.0)