Difference between Structure and Class

This is a discussion on Difference between Structure and Class within the C++ Programming forums, part of the General Programming Boards category; first of all, i want to say that i have ALREADY SEARCHED on it ... i want to discuss only ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    17

    Difference between Structure and Class

    first of all, i want to say that i have ALREADY SEARCHED on it ... i want to discuss only one thing with you people.

    here is the link that i used to get more related to this topic.

    http://www.geekinterview.com/question_details/3478


    My question is
    Does structure have "this" pointer in it?
    Last edited by Ken Fitlike; 01-11-2007 at 07:47 AM. Reason: removed unnecessary code tags enclosing link

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    structure and class in C++ differ only in 1 thing:
    all members of the class are private by default
    all members of the struct are public by default

    there is no other difference
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    there is another difference.
    one is named struct and the other is named class

    funny wont be my middle name for a long time

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Does structure have "this" pointer in it?
    Structures and classes don't have 'this' pointers.
    It is the member functions of the struct / class that have 'this' pointers.

    As vart says, a struct can have a member function just the same way a class can.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    About that, if C had structures which are the same as classes than C could also be Object Oriented?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by indigo0086
    About that, if C had structures which are the same as classes than C could also be Object Oriented?
    C structures are different from C++ structures
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by indigo0086
    About that, if C had structures which are the same as classes than C could also be Object Oriented?
    OO is a programming paradigm which has nothing to do with the language you use. you can code OO in C perfectly, its just that languages designed with OO in mind give you more expressiveness to code OO in. Think ctors/dtors, keywords/symbols for inheritance keywords for data hiding etc

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Oh I see. I always thought of OO as more of a language feature which kind of encapsulates a larger idea.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    It's actually the other way round. OO is a methodology based on a "larger idea" and that methodology can be used in any programming language. Some languages just include specific features that make it easier to apply the methodology directly.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    17
    Quote Originally Posted by Salem
    > Does structure have "this" pointer in it?
    Structures and classes don't have 'this' pointers.
    It is the member functions of the struct / class that have 'this' pointers.

    As vart says, a struct can have a member function just the same way a class can.
    That's what i was looking for.

    So you mean, structs don't have this pointer however member functions of structs have this pointer .. emm cool, but if someone ask me to prove it how could i? it will be really help full if you put some code over here.

    Actually i had the discussion with my friend and argued with these points that you people have posted here, but he said structs has this pointer, i was not sure and i keep silent ... however, i argue that if struct has this pointer then it should allow "inheritence", "polymorphism" which structs don't.

    One more difference could be. The objects of structs are much like a new custom data type, so they are created on stack and the of class objects are created on heap. so if there is a very big struct then it could be possible that it could make the stack overflow, however heap is managed by the OS and allow paging/defregmentation which makes LARGER classes to be in the program without any problem. what you say?

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by hYph3n
    emm cool, but if someone ask me to prove it how could i? it will be really help full if you put some code over here.
    Just tell them to read the language standard. It's in 9 §4:
    A structure is a class defined with the class-key struct; its members
    and base classes are public by default.
    That's all there is ever said specifically about structs in the C++ standard. They're otherwise identical to classes.

    Actually i had the discussion with my friend and argued with these points that you people have posted here, but he said structs has this pointer, i was not sure and i keep silent ...
    He means the right thing, but puts it the wrong way. Neither classes nor structs have a this pointer. It's the non-static member functions of both that have it.

    however, i argue that if struct has this pointer then it should allow "inheritence", "polymorphism" which structs don't.
    Yes, they do. And it's "inheritance".

    One more difference could be. The objects of structs are much like a new custom data type, so they are created on stack and the of class objects are created on heap. so if there is a very big struct then it could be possible that it could make the stack overflow, however heap is managed by the OS and allow paging/defregmentation which makes LARGER classes to be in the program without any problem. what you say?
    That's how it works in C#, but in C++ it's complete nonsense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > however, i argue that if struct has this pointer then it should allow "inheritence", "polymorphism" which structs don't.
    You're confusing 'this' (which is an invisible pointer to an instance of a struct/class, as received by a member function of that struct/class) with other hidden data inside the struct/class to manage things like inheritance and polymorphism.

    > but if someone ask me to prove it how could i?
    Does this help?
    Code:
    #include <iostream>
    struct foo {
      int a;
    };
    struct bar {
      int  a;
      bar ( void ) {
        a = 0;
      }
    };
    
    int main ( ) {
      foo    v1;
      bar    v2;
      std::cout << sizeof v1 << std::endl;
      std::cout << sizeof v2 << std::endl;
      return 0;
    }
    
    $ ./a.exe
    4
    4
    Just adding member functions to a struct/class doesn't add 'this' to the stored data.


    Some info on OO in C, with varying degrees of features in terms of programmer comfort.
    http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
    http://www.accu.org/acornsig/public/articles/oop_c.html
    http://www.eventhelix.com/RealtimeMa...mming_in_c.htm
    http://ootips.org/oop-in-c.html
    http://www.bolthole.com/OO-C-programming.html
    http://ficl.sourceforge.net/oo_in_c.html
    http://sunsite.uakom.sk/sunworldonli...ol-10-ooc.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    As a matter of fact, discussion of the "this" pointer is in Section 9.3.1 of the C++ standard, the first paragraph of which says;
    In the body of a nonstatic (9.3) member function, the keyword this is a non lvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.
    and a note at the start of the second para of Section 9.4.1 says this (which is what I suspect CornedBee was referring to).
    [Note: a static member function does not have a this pointer (9.3.2). ]
    One simple way to demonstrate that a struct does not have a "this" pointer is this code;
    Code:
    struct X
    {
         // whatever members you like
    };
    
    int main()
    {
        X x;
        X *p = x.this;
    }
    which will not compile because this is not a member of x.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure vs. class
    By $l4xklynx in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 02:00 AM
  2. Abstract class (IDispatch) in a C structure
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2008, 07:38 AM
  3. help with class structure assignment
    By Crcullen3916 in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2007, 02:51 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Deleting Data within a Structure or class
    By TankCDR in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21