10

I don't usually write this way but I've seen one code base where it's almost everywhere, e.g:

class prettyClass
{
    // just class stuff
};

int main()
{
    class prettyClass obj; // class?
    return 0;
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
ClownieAdd
  • 121
  • 6

1 Answers1

23

In C, the tags of structures live in a different namespace to other identifiers. So unless one introduces an alias, they must write struct T for the type name. In C++ (where class and struct are of the same underlying concept) it was made so that the tag is automatically introduced as a regular identifier.

However, there is one behavior in C that had to be accounted for. You could have something like this:

struct prettyThing
{
  // data
};

void prettyThing();

And there's no ambiguity, due to the different namespaces. So C++ preserves this behavior. One could also have:

class prettyThing
{
   // members
};

void prettyThing();

Without ambiguity, the class name is hidden by the other identifier. It is only accessible with an elaborated-type-specifier, i.e. class prettyThing. That's one practical reason for why one may write the declaration as you see. And it's not academic, consider the POSIX stat and struct stat pair.

But... unless you are interfacing with C code that was originally written with the C notion in mind, there is no reason to write new C++ code that way.

One may wonder why it wasn't kept the same for types declared with struct and simply disallowed for types declared with class. To understand that, it's best to reiterate and remember that classes and structures are really meant to be the same thing in C++ (save for default access specifier). The following is all valid C++:

struct thing1; // forward declare

class thing1 {
  // define
};

struct thing2 {
   // define
};

class thing2 object;

Either keyword can be used in the elaborated-type-specifier, they are completely interchangeable in standard C++1.


1 - Microsoft ABI behaves as a bit of an oddball here, though.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • "In C++ (where class and struct are the same)" `Struct` and `class` are almost the same. Default member visibility is different :-) – A. Ocannaille Jun 25 '21 at 13:19
  • 1
    @A.Ocannaille - If you want to go into such pedantry, then visibility is the wrong word. All members are *always* visible, with access checking as a seperate and last step. It's been the subject of many a question. As an example: https://stackoverflow.com/q/1201295/817643 – StoryTeller - Unslander Monica Jun 25 '21 at 15:06
  • Indeed, visibility is not the perfect term. Yet, if one must defines in one word only the difference between class and struct, it is, imho, the best one. OP Seems to be a begginer, stating "`class` and `struct` are the same" is wrong while defining visibility as the difference between `class` and `struct` is only simplifying. IMHO, for future readers, it was worth editing. Thank you for that BTW =) – A. Ocannaille Jun 29 '21 at 09:05
  • 2
    @A.Ocannaille - We'll have to agree to disagree. There's plenty of questions flowing in by people thinking a struct has its own category in type system when it doesn't. The "imperfect" term does far more harm than a sentence that blows one's expectations out the water. – StoryTeller - Unslander Monica Jun 29 '21 at 10:43