0

Possible Duplicate:
What are the differences between struct and class in C++

In C++ is there any reason to use a Struct inside of a Class outside of making your own linked list or b-tree?

I've taken a few programming courses in college, but haven't really thought about this until now. It seems like there wouldn't be any benefit from using a struct inside of a class, but I don't have enough experience to know what situations really require certain things. I'm hoping that you experienced programmers can shed some light on this for me.

Community
  • 1
  • 1
JeramyRR
  • 4,273
  • 3
  • 20
  • 20
  • @JohnDibling I don't think so. This is more of a rationale question. – Michael Price Oct 24 '11 at 19:36
  • Might I also suggest that someone add an 'oop' tag to this question, as it is essentially a question about object-oriented design. – Michael Price Oct 25 '11 at 01:02
  • 1
    It's pretty irritating that this was closed as a duplicate. It is very clearly not a duplicate. The question here is "In C++ is there any reason to use a Struct inside of a Class outside of making your own linked list or b-tree?" whereas the other 'question' is "Now I'd like to learn the differences between a struct and a class in (unmanaged) C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design." In what language are those two questions the same? – Michael Price Oct 25 '11 at 13:54

4 Answers4

3

Yes. When you want to model a complicated object which contains subobjects that are internal to the implementation of the parent object is one excellent case.

There are also many design patterns that can be implemented using such a technique (such as Observer and Delegate).

Michael Price
  • 8,088
  • 1
  • 17
  • 24
  • Also, in C++, structs and classes are equivalent in all manners except the default visibility of members and methods. In structs, the default is `public` and in classes it is `private`. Just something useful to know. – Michael Price Oct 24 '11 at 19:34
  • Oh, and the fact that it can affect derivation. See the question that @JohnDibling posted a link to above. – Michael Price Oct 24 '11 at 19:37
1

Structs and classes are the same thing.

In C++ is there any reason to use a Struct inside of a Class outside of making your own linked list or b-tree?

It's a silly question. Of course there is. Inner types are a tool, when you have a problem that requires it, you use it. Iterators, for example, are usually made to be inner types (not that they have to be, but it keeps the type implementation in one place).

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
0

There are an infinite number of things you can do in C++, so yes, some of them would be best implemented using a struct inside a class.

One reason people use structs is to build an array of them, which makes keeping track of related variables very easy. So one example would be a class that can perform operations on a list of related variables. You are correct that making a linked list is one example where this would be useful.

Another example would be a simple address book or transaction list, where you want to keep separate books that can perform operations on their members.

Chriszuma
  • 4,464
  • 22
  • 19
0

Here is a complex scenario: records of sub-records.

struct Title_Record
{
    unsigned int id_title;
    std::string  title_text;  // std::string is actually a struct / class.
};

struct Category_Record
{
    unsigned int id_category;
    std::string  category_text;
};

// A record of [id_ingredient, id_title, id_category],
class Ingredient_Entry
{
    unsigned int id_ingredient;
    Title_Record  title;        // Use ID field only for storing in database (foreign key).
                                // But also contain the record for local access.
    Category_Record  category;  // Use ID field only for storing in database (foreign key).

. };

Every ingredient entry has a title and a category. The title and categories are separate records so that ingredients can share common titles and categories.

Usage: Title_Record title_table[30]; // Database table of 30 titles. Category_Record category_table[30]; // Database table of 30 categories.

Remember that the keywords struct and class only differ in their default accessibility.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • This has me a little confused. Why would you use the Title_Record struct here in stead of just putting id_title and title_text as a member of the Ingredient_Entry class? – JeramyRR Oct 24 '11 at 20:06
  • @JeramyRR: See my edits. This is best thought or drawn as the structure as a whole horizontal with the sub-records drawn vertically. The Ingredient_Entry record shares ID fields with the sub-records. – Thomas Matthews Oct 24 '11 at 20:30