CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    struct questions.

    here they are:

    1.Can a struct inherit from another struct?
    2.Can a struct inherit from another class?
    3.Can a struct be the base of another class?
    4.Can a struct implement an interface?

    A one line explanation for each, instead of a simple yes or no would help a lot.

    Thanking all in anticipation.

    Regards
    I

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: struct questions.

    In C++, structures and classes are identical in all respects but two:
    1. Default member access; public for structures, private for classes.
    2. Default inheritance; public for structures, private for classes.

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: struct questions.

    This sounds like homework to me.

    The differences between structs and classes have been discussed probably hundreds of times on this forum. A search is in order, perhaps?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    Re: struct questions.

    mm..it is kind of homework.

    but on second thoughts I should not have put up all the four questions. I kinda know the difference between structs and classes, don't know if they can be mingled in a hierarchy.

    so just someone tell me if a struct can inherit from or form the base of a class.

    thanks.
    I

  5. #5
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: struct questions.

    Quote Originally Posted by indrajit_p1
    so just someone tell me if a struct can inherit from or form the base of a class.
    What part of my post did you not understand? Whatever you can do with a class, you can do with a structure...

    P.S.
    It's not that hard to try it yourself, you know.

  6. #6
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    Re: struct questions.

    Quote Originally Posted by Plasmator
    P.S.
    It's not that hard to try it yourself, you know.
    Hey Thanks! that option somehow skipped my mind. probably because I have written much less C++ code than I have read about the language.

    Thanks again.
    I

  7. #7
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: struct questions.

    Quote Originally Posted by Plasmator
    What part of my post did you not understand? Whatever you can do with a class, you can do with a structure...

    P.S.
    It's not that hard to try it yourself, you know.
    Yeah I agree. That's why we suggest you post a problem with the code in question so that you know yourself that it doesn't work before posting anything. I've learned that following this rule can greatly reduce the need for posting on the forums.

    I've submitted an FAQ on the difference between classes and structs since we often find ourselves repeating it. If it is accepted then we can just give people a link.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: struct questions.

    The only difference between class and struct is:

    Member of classes, inheritance is private by default.
    Member of structs, inheritance is public by default.


    If you read any other differences - it's a core crap!

    Yes, when you mix unions with either of these, there would be complixities and limitations.
    Last edited by Ajay Vijay; May 17th, 2008 at 02:18 AM.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: struct questions.

    The only difference between class and struct is:

    Member of classes are private by default.
    Member of structs are public by default.

    If you read any other differences - it's a core crap!
    No, Plasmator is correct. The C++ Standard states: "A structure is a class defined with the class-key struct; its members and base classes are public by default."

    Thus, another difference between classes and structs is that classes use private inheritance by default and structs use public inheritance by default.

    Oh, and then there are the trivial differences like difference in spelling between "class" and "struct"
    Last edited by laserlight; May 17th, 2008 at 02:17 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: struct questions.

    Thus, another difference between classes and structs is that classes use private inheritance by default and structs use public inheritance by default.
    You did not read it properly, I also mentioned it!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: struct questions.

    You did not read it properly, I also mentioned it!
    Oh, okay. I had the impression that you were contradicting what has already been said since indrajit_p1 seemed satisfied with what was already stated.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    Re: struct questions.

    Quote Originally Posted by laserlight
    The C++ Standard states: "A structure is a class defined with the class-key struct; its members and base classes are public by default."
    amidst all the crossfire of brilliant ideas and all that wisdom, this is the first time someone gave a REAL answer to my real question (which I made clear a little late). since the standards say that "a structure is a class", that answers my querry "if they can inherit from one another ( a class and a struct)".

    I tried it on my g++ (after being adviced by Plasmator), and it let's me instantiate a struct derived from a class which is in turn derived from a struct, and also call a function on it without as much as a whimper. but that would have left me with "my g++ lets me do it" as an answer when faced with that question in a formal discussion (and I said it was kind of a homework - really from a set of possible questions for a Microsoft, USA telephonic interview handed to me by my agent).

    Thanks laserlight.

    p.s.: and that was a small line from the standards, not worth surfing to a brand new internet URL, is it?

    p.s.: I don't know where to say this, but as a rule of the thumb, people should be clear about what the questioner is asking and how much (and which much) of it they can and are REALLY answering and clearly state the later in their post. it is far too often that I find people not caring to clarify themselves on the first part (like saying "this is the difference between struct and class" in reply to "if they can be mingled in an inheritance hierarchy". though it's clear now that they are not, they could, by all means, be two completely different things which share most of their properties, for someone who is not informed enough) and almost always not giving a single penny for the second. -Regards/Indrajit
    Last edited by indrajit_p1; May 17th, 2008 at 10:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured