Conversation started May 26, 2020 at 12:22.
May 26, 2020 12:22
Does a struct in c++ work the same as a struct in C#?
nwp
nwp
It doesn't.
struct and class are almost the same in C++.
They merely differ in visibility defaults.
nwp
nwp
If you want serious answers about C++ consider using the Q&A room.
@nwp Whaat? I thought this was the main C++ chat room?
The lounge is casual, not serious.
May 26, 2020 12:25
I see.
nwp
nwp
It's a lounge to recover from the mental trauma caused by C++ and for hanging out. The other one is for C++ Q&A.
Here is busier so I'll try.
@nwp I can imagine. I thought JavaScript code does a lot of magic, but wow C++ might be worse.
My question is:
C# classes have reference semantics, C# structs have value semantics. C++ has no such distinction. C++ classes and C++ structs both have value semantics. If you need reference semantics, use (smart) pointers.
What is the difference between a struct and an object since both are named containers for values, and both need to be instantiated?
nwp
nwp
That is really a question for the other room.
May 26, 2020 12:27
I don't understand the question. You can have struct objects.
@fredoverflow Okay, thanks. What do you mean reference semantics.
@fredoverflow Let me rephrase it:
What is the difference between instantiating a struct and instantiating a class since both instances are named containers for values, and both instances need to be instantiated?
@MyWrathAcademia In C++? There is no difference regarding instantiation.
Members and base classes are constructed as private by default in classes, whereas they are public by default in structs.
May 26, 2020 12:43
@fredoverflow I just watched your video. What a great little introduction to C++. Do you make videos like this that get someone already familiar with Java and C Sharp to learn C++ quickly? It may be more effective than reading one of those huge C++ books.
By the way, you're German, yes? You have a similar accent to my friend.
@TelKitty Thanks. So except for those differences, a struct is exactly the same as a class?
nwp
nwp
It's better to think of the struct keyword as making a class. You cannot distinguish structs from classes because they are the same thing. That class exists at all is a historic error.
May 26, 2020 13:01
@nwp great. I'll keep that in mind.
I'll think of the struct keyword as an alias for the class keyword.
nwp
nwp
Which is not quite correct because of the default visibility.
You can think of struct as an alias for class { public:. Except you have to fit the name in there and inheritance.
@nwp Okay. I noticed that in C Sharp unless I inherit from an instance of the same struct I have to initialize any fields and properties in the constructor. Is this the same in C++?
nwp
nwp
Kinda. You have to initialize things before you read from them, but it's not enforced by the language. You just get more or less screwed if you forget.
May 26, 2020 13:58
@nwp Thanks, but what I mean is even when I am not reading from an instance field or accessing it via an instance property, in C Sharp I still need to initialize all fields because all fields and properties in a C Sharp struct must be fully assigned before control is returned to the caller of the struct's constructor.
nwp
nwp
There is no such rule in C++.
Okay, visual studio is giving me an error with that warning.
 
Conversation ended May 26, 2020 at 14:01.