Thread: C/C++ question on struct definitions

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    C/C++ question on struct definitions

    Hi,

    This might very well be a C question. I am posting it here because it refers to some code in Bruce Eckels's 'Thinking in C++'. Apologies if this causes any inconvenience.

    There is a struct definition, and use thereof, on p.248-250 that confuses me.

    Within struct Stack, he defines the nested struct:
    Code:
    struct Link {
       void* data;
       Link* next;
       void intialize(void* dat, Link* nxt);
    }* head;
    Then, in the associated function Stack:ush(void* dat);
    he creates a Link like this:
    Code:
    Link* newLink = new Link;
    newLink->initialize(dat, head);
    head = newLink;
    There are a couple of things that I don't follow here.
    Firstly, he has not needed to call Link like this:
    Code:
    struct Link newLink......
    I was sure that a struct had to be initialized with an explicit struct definition unless it was typedeffed into the word between } and ; at the end of the struct. Was I completely wrong?

    Does he say on p.230-231 that this is a difference between C and C++ ? (I am not confident enough at either to tell for sure).

    Secondly, the word between } and ; here is * head. But * head is not a type definition in this case. Rather it is a particular instance of Link, namely the access point to the single-linked list for the container class. He says this is a legacy from C.

    *head in this case, seems to be just a particular instance of a Link. Is this what is taking place?

    Apologies again if this is in the wrong place. But part of the problem is that I am not sure what is C and C++ here. It is a nice example in Eckel, and I would like to use similar code myself, but I don't yet understand it.

    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's C++ because of the use of new.

    In C the struct keyword is required; C++ does not require the struct.

    You are correct on the second point.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Hi,

    Thanks for your help, and of course you're right vis a vis "new" - my slip there.

    Regarding the last point, is this something you can do in C as well as in C++ and, in C++, does it work with classes as well as structs?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes, and yes. You have always (for a suitable definition of "always") been able to declare variables of a struct type when you were declaring the type.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by junket View Post
    Hi,
    ... is this something you can do in C as well as in C++ and, in C++, does it work with classes as well as structs?
    But of course in C you can't declare a function inside a struct, and the pointer declaration inside the struct would have to include the struct keyword.

    In C++, you should get used to the idea that a struct is almost exactly the same as a class. The only difference is that in a class all members are private by default, and in a struct all members are public by default. A C++ struct can contain constructors, a destructor, and other functions, and its members can be explicitly declared as private or public the same as you would in a class.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Great, thanks alot for all your help.

    It seems like there is alot of ways to do things in C++, maybe because some things were preserved for backwards compatibility, but then people decided they liked the choice anyway.

    Which is all good by me.

    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. general question about how struct works.
    By nullifyed in forum C Programming
    Replies: 14
    Last Post: 06-21-2010, 06:03 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM