C history question. Struct member functions.

In summary, the conversation discusses the difference between structures and classes in C++, with the main difference being the default visibility of members. The concept of member functions in structures is unique to C++, and has been a part of the language since at least 1998. It is not present in C, but C structs can have pointers to functions. The exact timeline of when this functionality was introduced is unclear.
  • #1
uart
Science Advisor
2,795
21
Old Pascal/Delphi programmer here with a question about c. I can kinda make my way around in c/c++ but I'm no expert.

I always thought the "struct" data type in c was just a mixed data type similar to "record" in Pascal. Now I find that "struct" in c++ can have member functions and basically do everything that "class" can do.

I was just wondering when this functionality entered or if it was always there? Was it something that was added in c++ (so not in c) or was it a feature added at some other time (or even was it there from the beginning of c)?

Thanks. :)
 
Technology news on Phys.org
  • #2
Basically, there is no difference between structure and Class in C++. The only difference is that the default visibility of members in structure is public.

I think, once the concept of class was used in C++, they realized class was just an extension to structure, and they felt they should ... Ok, leave out, I am just fantasizing. :). I really don't know anything about the developmental stages of C++. Sorry.
 
  • #3
I_am_learning said:
Basically, there is no difference between structure and Class in C++. The only difference is that the default visibility of members in structure is public.

I think, once the concept of class was used in C++, they realized class was just an extension to structure, and they felt they should ... Ok, leave out, I am just fantasizing. :). I really don't know anything about the developmental stages of C++. Sorry.

Yeah I've recently been refreshing my knowledge of c++ and that's when I notice this. "Stuct" seems to be pretty much the same as "class" except for differences in the defaults for public/private. I understand that, I'm just wondering when this functionality entered the language. Was it something added to c++ or did it come earlier/later than that?
 
  • #4
C has no notion of member functions. Member functions in structs are purely a C++ thing, and have been part of C++ since at least the first ISO standard around 1998, probably earlier.

(as far as I know)

I didn't keep my old version of Bjarne Stroustrup's The C++ Programming Language from the early 1990s, which would probably say what the situation was at that time.
 
  • #5
As mentioned, C doesn't support member functions, but C structs can have pointers to functions, which is similar.
 
Last edited:
  • #6
Thanks for the info jtball and rcgldr. :)
 

Related to C history question. Struct member functions.

1. What is a struct in C?

A struct in C is a user-defined data type that allows you to group different variables together under one name. It is similar to a class in other programming languages.

2. What are member functions in a struct?

Member functions in a struct are functions that are defined inside the struct and can only be accessed by instances of that struct. They are used to manipulate the data within the struct.

3. How are member functions declared in a struct?

Member functions in a struct are declared using the same syntax as regular functions, but they are placed inside the struct definition and are preceded by the struct name and a dot operator. For example: structName.functionName();

4. Can a struct have multiple member functions?

Yes, a struct can have multiple member functions. This allows for more complex data manipulation and organization within the struct.

5. Can member functions in a struct have parameters?

Yes, member functions in a struct can have parameters. This allows for more flexibility in how the data is manipulated and allows for different values to be passed into the function.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
6
Views
947
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
705
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
2
Views
427
  • Programming and Computer Science
Replies
1
Views
949
  • Programming and Computer Science
Replies
21
Views
1K
Back
Top