TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: PAUL DRUGGITT
from: CAMERON CLARK
date: 1997-05-22 18:41:00
subject: Re: passing structures to functions

PD>  Can anyone tell me how to pass structures of different sizes to a
PD>  function ?
    Use classes & polymorphism to do this.
PD>  struct RECORD {
PD>      int type;
PD>      char desc[26];
PD> 
PD>  } DATA;
    class BaseClass {
      int type;
      int desc[26];
      // data common to all sub classes
    }
    class RECORD : public BaseClass {
    }
PD>  struct BIGGER_RECORD {
PD>     int type;
PD>     char desc[26];
PD>     char address_1[40];
PD>     char address_2[40];
PD>     int record_number;
PD> 
PD>  } BIGGER_DATA;
    classs BIGGER_RECORD : public BaseClass {
     // data here
     char address_1[40];
     char address_2[40];
     int record number;
    }
PD>  void display_data(struct RECORD *rec_name);
    void display_data(BaseClass& rec_name);
PD>  void (display_data(struct RECORD *rec_name)
PD>  {                  ^^^^^^^^^^^^^
PD> 
PD>     if(rec_name->type <= 100)
PD>         printf("type = %d", rec_name->type);
PD> 
PD>     else
PD>         printf("type is more than 100");
PD> 
PD>     printf("\ndesc = %s", rec_name->desc);
PD>  }
void display_data(BaseClass& rec_name) {
  if (rec_name.type <= 100)
     printf();
  else
     printf();
  printf("", rec_name.desc);
}
    The compiler uses runtime binding to see what kind of "BaseClass"
    you pass to the function: RECORD, or BIGGER_RECORD.
    This is not polymorphism, persay, but is very close to the idea
    of it.
PD>  Now, is it possible to write a single function that will accept ANY
PD>  structure as a parameter, stating which member to compare?
    Yes. See the above display_data() function.
    All descendants of BaseClass can be used in the function. Once again,
    this is the beauty of runtime binding. It would be better to define
    display_data() funtion in BaseClass and allow for overriding by assigning
    the virtual attribute to it.
--- GEcho 1.00
---------------
* Origin: Digital OnLine Magazine! - (409)838-8237 (1:3811/350)

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™.