LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   calling global structs (https://www.linuxquestions.org/questions/programming-9/calling-global-structs-215803/)

LuderForChrist 08-10-2004 10:34 AM

calling global structs
 
Im the progress still of porting C++ to C, yes yes yes I know, but ran across another problem.

I'm making each C++ class into a C struct,
and I need to know how to inherit another struct like one would inherit a class in C++.

Thanks guys! (and gals)

itsme86 08-10-2004 10:42 AM

You can't meld structs together, but you can nest them:
Code:

struct base_struct
{
  int some_stuff;
  char *some_more_stuff;
};

struct lesser_struct
{
  int yet_more_stuff;
  struct base_struct base_stuff;
};

Is that what you were looking for?

LuderForChrist 08-10-2004 10:47 AM

Yea I believe that it is! Let me give it a try. thanks

LuderForChrist 08-10-2004 11:11 AM

mmmmm let me see if I got this

FILE 1
Code:

struct THEA A
{
    int blah ;
    int more crap;
};

FILE2
Code:

struct THEB B
{
    int look;
    struct THEA A;
}

............ later function the struct THEB B, was passed in

A->blah = the_result;
        OR
B->blah = the_result

neither work so ...... mmmmmmmmmmm what am I doing wrong

itsme86 08-10-2004 11:29 AM

You need to declare your structures like:
Code:

struct THEA
{
  int blah;
  int more_crap;
};

struct THEB
{
  int look;
  struct THEA A;
};

int main(void)
{
  struct THEB B;

  B.look = 1;
  B.A.blah = 2;
  B.A.more_crap = 86;

  return 0;
}

You can't use one of THEA's members directly using B. You have to step through the nesting like that. You could of course pass &B.A to a function then use A->blah or A->more_crap in that function. But you can't do something like B.my_crap in my example above. You have to do B.A.more_crap.

itsme86 08-10-2004 11:43 AM

Here's a simple example. Say you're tracking book sales. Every book has a name and a number of pages and so forth. So you set up a book struct like so:
Code:

struct book_base
{
  char *name;
  int num_pages;
};

Now, normally you'd set up array of struct book_base, one element for each book you're tracking. So say you had 3 books. You might do something like:
Code:

{
  struct book_base books[3] =
  {
    { "Book 1", 213 }, { "Book 2", 99 }, { "Book 3", 792 }
  };
}

Now let's say you have 50 of each book and you want to track where they are. You could do something like this:
Code:

struct indiv_book
{
  int sold;              // 0 = not sold, still in store; 1 = sold
  time_t sell_date;//  0 if not yet sold otherwise time_t value of when sold
  struct book_base *book; // Pointer to book information
};

Then you might do something like:
Code:

{
  struct indiv_book book[150];  // 50 each of 3 books

  book[0].sold = 0;  // Not sold yet
  book[0].sell_date = 0; // Set to time(0) when sold
  book[0].book = &books[1]; // This particular book is a "Book 2".
}

Then you could see the name of book[0] that you're tracking by doing: printf("%s\n", book[0].book->name);

Or you could check how many pages book[0] has by looking at book[0].book->num_pages.

Clear as mud? :)

LuderForChrist 08-10-2004 12:40 PM

almost but Im trying to set a variable from the first struct not something from the second struct.

itsme86 08-10-2004 12:55 PM

Then you would do like: book[0].book->name = "new name";

EDIT: Or you could do books[1].name = "new name"; or course if you wanted to change it directly without going through an indiv_book struct instance.

bruce ford 08-11-2004 12:42 AM

hi!

ever tried something like this:

Code:

struct base_struct
{
  int some_stuff;
  char *some_more_stuff;
};

struct lesser_struct : public base_struct
{
  int yet_more_stuff;
};

(Thanks itsme86 for the example)
I have learned that in C++, structs are EXACTLY like classes except that the default visibility is public instead of private.
So, you also could have used classes instead.

So long...
bruce


All times are GMT -5. The time now is 06:46 PM.