Thread: struct is a class???

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    struct is a class???

    I noticed that the struct acts exactly like a class, so what's the difference, except the fact that unlike the class, the struct is public by default?

    Thank you.
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct point
    {
       private:
          int a, b;
       public:
          point()
          {
             cout<<"buidling...\n";
          }
          ~point()
          {
             cout<<"dead!!!\n";
          }
          void func(void);
    };
    
    void point::func(void) {
       cout<<"something here...!!!\n";
    }
    
    int main()
    {
       struct point p1;
    
       p1.func();
       return(0);
    }
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    That is the only difference. I use struct when it doesn't need any other methods other than a constructor and destructor, and I always leave all members public. All other times I use class.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    look at my code... you can define public and private and other functions as well... why is that?

    also, the compiler allows me to do this:
    Code:
    class point p2;
    even if the point is a struct, what the hell is going one here?
    Last edited by Devil Panther; 09-09-2005 at 10:26 AM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is because they are basically the same. The struct keyword was inherited from C, where they have a slightly more restrictive meaning and use. In C++, structs were given the same powers as classes, so you can basically use them interchangably as long as you set the access specifiers you want.

    A common practice is to do what Ancient Dragon does and use structs for holding only public data. This is simply because that is what structs look like in C.

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I see... but what about:
    also, the compiler allows me to do this:
    Code:
    class point p2;
    even if the point is a struct, what the hell is going one here?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    While on the subject I have a question: Is it possible to inherit from structs and do they support polymorphism in the way classes do?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Yes, they are the *SAME* thing except for teh 1 listed different.

    SAME THING!

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> also, the compiler allows me to do this

    My compiler gives me a warning. It would appear that it doesn't give an error because it doesn't matter - class and struct are the same thing. It basically ignores that part. Besides, it shouldn't matter which you use because putting struct or class there is not necessary. Just use
    Code:
    point p2;

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I know I don't need to place class / struct, but I prefer to do that...
    the question is, can I define a class and a struct with the same name?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Devil Panther
    the question is, can I define a class and a struct with the same name?
    Did you try it? Experimentation teaches you a lot

  11. #11
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Well just think about what happens when you try to instantiate the class or struct...

    That's what I did. And then I tried it. Yep, I was right.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Well so did I... The compiler doesn't see the difference between the struct and the class... weird...
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Devil Panther
    Well so did I... The compiler doesn't see the difference between the struct and the class... weird...

    there's nothing "wierd" about it -- the language was designed that way on purpose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM