Is my understanding of ECS correct?

Started by
28 comments, last by Acosix 4 years, 11 months ago
1 hour ago, Acosix said:

Well a class is made  for making an object. A struct can not be an object.

That's not true as stated. Classes and structs are the same in C++ except for default visibility (which isn't a qualitative difference). Everything you can do with one, you can do with the other, and you can instantiate objects from structs just as you can from classes.

Also, while classes/structs are often (perhaps usually) intended for instantiating objects, that's not always the case. Classes/structs can be used for other purposes as well.

Obviously this is all tangential to the topic of the thread, but confusion regarding the difference between classes and structs in C++ isn't uncommon, so I think it's worth clarifying when it comes up.

Advertisement

Well the difference between class and a struct?

Well objects are designed to be encapsulated, so I prefer classes as the primary access is private...

And by the ISO C++11 standard of 2011, classes must have constructors and destructors, unlike data structures.

Quote

Well objects are designed to be encapsulated, so I prefer classes as the primary access is private...

It's not so much 'primary' access as it is 'default' access (although I suppose you could use the word primary in the sense that with classes, private is the 'first' access mode in the class body, barring any access specifiers).

In any case, I'm just addressing a technical point here and trying to counter the common misconception that classes and structs are qualitatively different in C++ (your comment that structs can't be objects led me to believe you might have this misconception). It's certainly true that classes and structs are often used differently by convention, and I think your usage is probably in line with common convention. I'm just pointing out that there's no qualitative difference between the two (unless you consider default visibility to be qualitative, which I wouldn't).

Quote

And by the ISO C++11 standard of 2011, classes must have constructors and destructors, unlike data structures.

I'm not sure what you mean by 'data structures', but I think whatever you're referring to here is either something different from what we're talking about, or you're mistaken about it. I'm interested in what you mean though, so if you could provide a link to what you're referring to, we might be able to clear it up.

There is no difference between a struct and a class in C++ except for the default access to members and methods. You can use either in exchange for the other, except for the access. Both have constructors and destructors, and if you don't type them in the compiler generates them.

1 hour ago, Zakwayda said:

I'm not sure what you mean by 'data structures', but I think whatever you're referring to here is either something different from what we're talking about, or you're mistaken about it. I'm interested in what you mean though, so if you could provide a link to what you're referring to, we might be able to clear it up.

I have read in the Bjarne Stroustrup's(the inventor of C++) The C++ Programming Language(4th Edition) which also explains 2011 standard, called C++11 ISO standard.

struct is basically meant for making data structures, whereas a class is meant for making user-defined data types(language-defined data types are int, bool, etc. ... in other words the IDE bolded font data types).

"A struct is simply a class where the members are public by default."

Stroustrup, The C++ Programming Language, 4th ed., page 206.

1 hour ago, Acosix said:

I have read in the Bjarne Stroustrup's(the inventor of C++) The C++ Programming Language(4th Edition) which also explains 2011 standard, called C++11 ISO standard.

struct is basically meant for making data structures, whereas a class is meant for making user-defined data types(language-defined data types are int, bool, etc. ... in other words the IDE bolded font data types).

Looks like Green_Baron covered it, but since this is a common (and surprisingly persistent) misconception, I'll go ahead and offer some further reinforcement.

Here are a couple links to sources that I think are credible:

https://en.cppreference.com/w/cpp/language/class

The above is a reference page for class declarations that says the class and struct keywords are identical except for default member and base class access.

https://isocpp.org/wiki/faq/classes-and-objects#struct-vs-class

The above is an entry from a popular C++ FAQ that (according to the Wiki at least) includes material from Bjarne Stroustrup, Marshall Cline, and perhaps others. It states that default visibility is public for struct and private for class, but that the two keywords are otherwise functionally equivalent.

Keep in mind that if you find instances of people talking about using classes and structs differently (including perhaps in Stroustrup's book, based on what you've said), they're most likely just talking about convention, not language specification.

If the references and quotes I and others have provided aren't sufficient, here are a couple ways you can challenge your current understanding of things:

- Try to find a credible C++ reference or source that doesn't just describe how class and struct are often used conventionally, but specifies actual technical differences between them other than default visibility.

- Write a test program using classes that uses a variety of features such as explicit constructors and destructors, private and public data and methods, inheritance, and so on. Once it builds and runs successfully, do a find-and-replace and replace all instances of 'class' with 'struct' (you may have to fix some things, for example if you're using the class keyword with templates). After the find-and-replace, build and run again. Everything should work the same as it did before.

If you find a credible reference that says something different from what we've been saying here, or come up with a code example that shows that the two keywords aren't functionally equivalent, please post the info here, as it'd be interesting and informative to see. (Although I'm confident in what I've said here, I always leave open the possibility that I've overlooked something.)

5 minutes ago, Zakwayda said:

Keep in mind that if you find instances of people talking about using classes and structs differently (including perhaps in Stroustrup's book, based on what you've said), they're most likely just talking about convention, not language specification.

Well the ISO standard is just as important as the raw general usage(in this case, code that compiles).

5 minutes ago, Acosix said:

Well the ISO standard is just as important as the raw general usage(in this case, code that compiles).

I'm not sure what you mean by the above. If there's something in the standard you think is relevant, perhaps you could provide a quote or other reference.

In any case, we've gone off on a tangent here (the original topic being ECS), so I'll just offer one more comment and try to leave it there.

To reiterate what I said earlier, I think if you want to show that class and struct differ functionally in ways other than default visibility, you need to provide a credible source and/or a code example that demonstrates the difference.

If however you're just saying that the keywords are often used differently as a matter of convention, that's true but also uncontroversial. It did seem that earlier you were saying the two were fundamentally different, so if now you agree they're the same apart from default visibility and that usage differences are a matter of convention, then I'd say the question has been resolved.

5 minutes ago, Zakwayda said:

To reiterate what I said earlier, I think if you want to show that class and struct differ functionally in ways other than default visibility, you need to provide a credible source and/or a code example that demonstrates the difference.

If however you're just saying that the keywords are often used differently as a matter of convention, that's true but also uncontroversial. It did seem that earlier you were saying the two were fundamentally different, so if now you agree they're the same apart from default visibility and that usage differences are a matter of convention, then I'd say the question has been resolved.

No, convention isn't a matter of a programming style, but best practices made by the inventor of the C++ programming language, Bjarne Stroustroup and the ISO C++ standard committee, which adds a new standard every 3 years that adds new functionality, deprecates bad functionality and 3 years after deprecation deletes the deprecated; and in which Stroustroup has participated many times.

To summarize ISO C++ standards(example years of every-3-year publication are 1998/2001/2004/2007/2011/2014/2017 and the next one is 2020),  are not about the random common programming practices, but adding functionality and correlated best practices.

To OP, why is this program Open Source?

This topic is closed to new replies.

Advertisement