TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: RICK ARDING
from: CLIFF RHODES
date: 1997-03-28 21:17:00
subject: Things

--> Rick Arding wrote to Cliff Rhodes <--
 
CR> struct Thing {
CR> int  Across, Down;
CR> char ThingChar;
 
CR> // Constructor
CR> Thing(int a, int d, char c) { Across = a; Down = d; ThingChar =
CR> c; } // ShowThing() member
CR> void ShowThing(void) { gotoxy(Across, Down); putch(ThingChar); }
CR> };
 
RA>I've experimented a little with OOP.  I have trouble understanding
RA>constructors and destructors so I leave them out.  I understand if
RA>you do this then C++ add them.
 
C++ will create a default constructor for you. In many instances, that
is acceptable--it does nothing. I generally like to provide a
constructor to at least initialize the data members. The constructor
gets called once when the object is created, so its a perfect time to
initialize. The destructor gets called once before the object is
destroyed, so it's a good time to tidy up any unfinished business.
 
RA>When I define ShowThing don't I need to use a :: symbol or something,
RA>or is it something that is not necessary?
 
In the above code :: is unnecessary since the function body is defined
inside the struct definition. If, in the struct, I had only
 
void ShowThing(void);   // Declare member ShowThing()
 
I would need
 
void Thing::ShowThing(void) { gotoxy(Across, Down); putch(ThingChar); }
 
outside the struct to define ShowThing() that is a member of Thing.
 
RA>I'm going to work with this. Any other people I
RA>talk to about C, tell me not to pass structures, just the fields from
RA>the struct.  Seemed like a waste not to pass the whole structure.
 
They are half correct. You do not want to pass the whole struct
because the compiler must make a copy of the whole thing to pass. But
you can pass a pointer to the struct. That means the compiler only has
to copy a few bytes to pass, not the whole structure. In C++ you can
also pass a reference, which behaves like you are passing access
directly to the structure. The structure isn't copied yet you can get
right at it.
 
struct S{ int a, b; };
 
void func1(S s)
{
   /* struct s passed as a copy. Not a good practice. Changes to
    * s are local to this function.
    */
   s.a = 0; s.b = 1;
}
 
void func2(S *s)
{
   /* s is a pointer to a struct given when func2() is called. *s is
    * a struct by dereferencing pointer s. Use the -> operator
    * to get from the pointer to a data member. Changing data members
    * affects the struct pointed to by s. So, changes to data members 
    * are not local.
    */
   s->a = 0; s->b = 1;
}
 
void func3(S &s)
{
   /* C++ only. s is a reference to a struct. s is essentially an alias
    * for the struct given when func3 is called. Use the . operator to
    * access data members because you are in effect dealing with the
    * passed struct directly. Changes to s are not local.
    */
   s.a = 0; s.b = 1;
}
#include 
int main(void)
{
  struct S s;
  s.a = 5; s.b = 10;
  func1(s);    // Pass struct s directly
  cout << "a = " << s.a << endl;   // a is still 5
  func2(&s);   // Pass a pointer to struct s
  cout << "a = " << s.a << endl;   // a will now be 0
  s.a = 3;
  func3(s);    // Pass a reference to struct s
  cout << "a = " << s.a << endl;   // a will now be 0 
  return 0;
}
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.