Thread: struct or class?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know. I didn't define the standard. But IMO private is best, as not to compromise the class contents, if anything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Which is a silly argument.
    If I want a public members, I add "public:" to the beginning of the class.
    Are you implying that sometimes you DON'T want public members? What is the point of a class with no public interface?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I have made private-only classes and declared friend classes to make sure only a specific class can use the object. Maybe not THE best idea, but it works, at least when there's no complications with private/public members and functions.
    Of course, I have lots of classes WITH public interface. And there's a pretty "public" before those functions/variables too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    hmm... then what's the default visibility for?
    When C++ was being designed backwards compatibility with C was a consideration (a mistake, IMO). So the semantics of "struct" has to remain the same, i.e. default public access. At the time it was thought that the default should actually be private. But since they couldn't change the semantics of "struct" they had to introduce a new keyword "class" to get a different default behavior.

    If backward compatibility with C had not been a consideration, we would probably have a language with only ONE class keyword, not two.

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    I have made private-only classes and declared friend classes to make sure only a specific class can use the object.
    If you have a class which is a helper to another class, it should be declared WITHIN that other class as a private member. Then you don't need to use friend.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is declared within that other class, and it's not a private member because it's instantiated within the code and do different purposes. Sometimes I push them into a vector, too. Which is private if I'm not mistaken.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    It is declared within that other class, and it's not a private member because it's instantiated within the code and do different purposes. Sometimes I push them into a vector, too. Which is private if I'm not mistaken.
    So basically it's a chunk of opaque internal state that can be passed around outside the class that uses it?

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think that if I understand that correctly, that it's basically a data type (declared private within the class), that's used internally by the class, passed around and messed with, then yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't know. I didn't define the standard. But IMO private is best, as not to compromise the class contents, if anything.
    I think you are missing the point of the very rule you use. Let's take a look at Stroustrup's glossary on struct: "class with members public by default. Most often used for data structures without member functions or class invariants, as in C-style programming."

    Let's take a look at what the Dictionary of Algorithms and Data Structures has to say about data structures and abstract data types:

    abstract data type: A set of data values and associated operations that are precisely specified independent of any particular implementation.

    data structure: An organization of information, usually in memory, for better algorithm efficiency, such as queue, stack, linked list, heap, dictionary, and tree, or conceptual unity, such as the name and address of a person.

    Since there are no associated operations specified in a data structure, it makes sense that some mechanism for specifying a data structure not restrict access to the values of that data structure. Consequently, struct, which has public visibility by default, is a good fit.

    On the other hand, a mechanism for implementing an abstract data type (it will no longer be abstract I suppose, haha) may wish to have restrictions placed on accessing the data values since the associated operations are already "precisely specified". class is a better fit than struct because of its default private visibility.

    As such, I think that the advice to "don't go around choosing just because of the default visibility" has some truth in it, but to say that "using structs/classes depending on the default access is silly" is silly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I use class for classes and add/change public, private and protected appropriately.
    If I simply need a big data structure, with only members to store data packed together, I use struct.
    It does not matter what the default visibility is. I don't choose struct if I want to add functions to a class. I don't choose class if I ONLY want member variables (OK, so maybe I do sometimes, if I think I might need to expand the functionality from just member variables to add functions and operators).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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