Thread: class/struct question

  1. #1
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    class/struct question

    Hi, I just found something to confuse me and nothing that would explain it to me. Seems like I got to ask another newbie question ...

    In a tutorial I found the following code:

    Code:
    struct CVector3 {
    	public:
    		float x, y, z;
    
    		CVector3() {}
    		CVector3(float fX, float fY, float fZ);	
    
    		CVector3 operator+(CVector3 vVector);
    		CVector3 operator-(CVector3 vVector);
    		CVector3 operator*(float fNum);
    		CVector3 operator/(float fNum);
    };
    At first it looked like a normal class, but then I saw the struct at the beginning.

    Is this just "bad code" or is there a real meaning to this? If I replaced the struct with a class, would it make any difference ?

  2. #2
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by Salem
    > If I replaced the struct with a class, would it make any difference ?
    The only difference - as far as I know - is that classes default to private, and structs default to public.

    Since this struct begins with public:
    Changing it to a class should have no effect.
    mhm, so it's the same ? What if it started with "private" ?

  3. #3
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    I seem to have some problems understanding this

    Does struct really declare a type like class does ? I always thought struct would declare an instance unless you use typedef.


  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "Does struct really declare a type like class does ? I always thought struct would declare an instance unless you use typedef."

    Yes, in C++ there is absolutely no difference between a struct and a class except for the default access, i.e. the access given to members or functions when you don't specify it.

    This declares a type:
    Code:
    struct Book
    {
         int location;
         int number;
    
    };
    This declares an instance:

    struct Book a_book(2000, 3);

    Or, this declares a type and an instance:
    Code:
    struct Book
    {
         int location;
         int number;
    }a_book;
    Last edited by 7stud; 08-03-2003 at 06:40 PM.

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Thanks, Salem and 7stud!

    There is just one more thing... what exactly does this thing do then?


    Code:
    typedef struct _AUTHENTICATE_MESSAGE {
        UCHAR Signature[sizeof(NTLMSSP_SIGNATURE)];
        NTLM_MESSAGE_TYPE MessageType;
        STRING LmChallengeResponse;
        STRING NtChallengeResponse;
        STRING DomainName;
        STRING UserName;
        STRING Workstation;
        STRING SessionKey;
        ULONG NegotiateFlags;
    } AUTHENTICATE_MESSAGE, *PAUTHENTICATE_MESSAGE;
    A variable _AUTHENTICATE_MESSAGE, a type AUTHENTICATE_MESSAGE and a type PAUTHENTICATE_MESSAGE that is a pointer to an AUTHENTICATE_MESSAGE type?

    Or maybe a type _AUTHENTICATE_MESSAGE, a variable AUTHENTICATE_MESSAGE and a pointer *PAUTHENTICATE_MESSAGE to a _AUTHENTICATE_MESSAGE type?

    If I'd follow 7studs example, the second paragraph would be the correct one, but somehow I got the feeling that the first one is correct. Or neither of them ?




    edit: ok, I think I got it:

    - _AUTHENTICATE_MESSAGE is the tag which I could use to create a variable using the struct keyword.... obviously something from the past so I don't need that anymore in C++...
    - AUTHENTICATE_MESSAGE is the type...
    - *PAUTHENTICATE_MESSAGE a pointer to a variable of that type...
    - there are no variables declared in that piece of code.

    It that correct now ?
    Last edited by darksaidin; 08-03-2003 at 07:12 PM.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by darksaidin
    Thanks, Salem and 7stud!

    There is just one more thing... what exactly does this thing do then?


    Code:
    typedef struct _AUTHENTICATE_MESSAGE {
        UCHAR Signature[sizeof(NTLMSSP_SIGNATURE)];
        NTLM_MESSAGE_TYPE MessageType;
        STRING LmChallengeResponse;
        STRING NtChallengeResponse;
        STRING DomainName;
        STRING UserName;
        STRING Workstation;
        STRING SessionKey;
        ULONG NegotiateFlags;
    } AUTHENTICATE_MESSAGE, *PAUTHENTICATE_MESSAGE;
    A variable _AUTHENTICATE_MESSAGE, a type AUTHENTICATE_MESSAGE and a type PAUTHENTICATE_MESSAGE that is a pointer to an AUTHENTICATE_MESSAGE type?

    Or maybe a type _AUTHENTICATE_MESSAGE, a variable AUTHENTICATE_MESSAGE and a pointer *PAUTHENTICATE_MESSAGE to a _AUTHENTICATE_MESSAGE type?

    If I'd follow 7studs example, the second paragraph would be the correct one, but somehow I got the feeling that the first one is correct. Or neither of them ?




    edit: ok, I think I got it:

    - _AUTHENTICATE_MESSAGE is the tag which I could use to create a variable using the struct keyword.... obviously something from the past so I don't need that anymore in C++...
    - AUTHENTICATE_MESSAGE is the type...
    - *PAUTHENTICATE_MESSAGE a pointer to a variable of that type...
    - there are no variables declared in that piece of code.

    It that correct now ?
    _AUTHENTICATE_MESSAGE is the original name of the structure.
    AUTHENTICATE_MESSAGE is a typedef for _AUTHENTICATE_MESSAGE.
    PAUTHENTICATE_MESSAGE is a typedef for _AUTHENTICATE_MESSAGE *.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Just so you know, these typdef things can get very complicated. The basic idea of a typedef is simple, but I've never seen a tutorial anywhere or an explanation of how to figure out the complicated ones that are allowed. The one you listed was a fairly simple one, and I couldn't figure it out. I've seen much more complicated ones too.

    Notice that the basic typedef for a struct ooks like this(which is different than declaring a type and a variable at the same time):
    Code:
    typedef struct Book
    {
         int location;
         int number;
    
    }B;
    
    int main()
    {
        B my_book;
    
        return 0;
    }
    Last edited by 7stud; 08-03-2003 at 08:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM