0

I was looking for a way to use C++ class in C code. I came across this C++ library in c. Going through the write noticed a method where class definition is declared as struct. I even confirmed it with below code offline.

#include <iostream>
using namespace std;
class shyam
{
  int a;
};
int main()
{
  struct shyam obj;
  return 0;
}

here we can see that class type shyam can be used to declare a structure obj of type shyam. I want to why this behavior exists and what are the potential use cases for it ?

Thank You !

  • Haven't you just read an article about a potential use case? – molbdnilo Sep 15 '21 at 08:27
  • This question does not duplicates [When should you use a class vs a struct in C++](https://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c) which is about **class definition** with `struct` or `class` keyword. This question is asking why **variable definition** with prepending `struct` or `class` keyword is valid, aka [elaborated type specifier](https://en.cppreference.com/w/cpp/language/elaborated_type_specifier). – VainMan Sep 15 '21 at 08:52
  • 1
    @VainMan the answer is more or less identical. It makes no substantial difference whether `shyam` was defined with using the keyword `class` or `struct`, and similarly for the elaborated type specifier you can use either of the two – 463035818_is_not_an_ai Sep 15 '21 at 09:09
  • The old link, 3 rd comment answers my question. Thank you ! – siva_uchiha Sep 16 '21 at 06:40

0 Answers0