简体   繁体   中英

Creating a struct and a class with the same name

Summary : What happens if you declare a struct and a class with the same name.

Details :

I'm reviewing some code and I saw a warning like this:

warning: struct 'foo' was previously declared as a class

When compiling with clang there were also a couple of places where clang added notes saying:

foo.h:29:1: note: did you mean struct here?
class foo;
^~~~~
struct

Obviously this is not a good coding practice to have a struct and a class with the same name. It looks like what happened is the developer was writing his own class and used a name that was already in use in another file that he was including and he did not notice that.

However, my question is will the compiler be able to tell the difference between the variables that were declared as class foo and the ones that were struct foo ?

Edit :

Actually what was happening was that the developer was using the class foo in a class bar that he had created. I think that in the meantime the place where the class foo was declared had been changed to a struct bar . So that was why the code was compiling. So I guess the answer to my question is that struct and class are interchangeable when declaring objects. Still I guess it's a good idea to use them consistently.

The keywords struct and class are largely interchangeable. Even if you write struct Foo , you have created a class type with the name of Foo . And that name must be unique within its scope (with a special exception for reasons of C compatibility, but which it is better to ignore).

In general all identifiers must be unique.

In particular, in Section 10.2.8 of the book "The C++ Programming Language", Bjarne Stroustrup states:

By definition a struct is a class in which members are public by default; that is

 struct s { ... 

is simply shorthand for

 class s { public:... 

There is no underlying difference between struct and class ; the former is mere syntactic sugar.

So, even if you could have identifiers with the same name (which, again, you cannot), there would be no distinction whatsoever between a struct and a class because they are the exact same thing.

无论定义的是类还是结构,标识符都必须是唯一的。

I would take @Alex Chamberlain and @James Kanze's comments as an answer:

There is nothing wrong with the code because declaration != definition : You can declare a type as a class , struct or union and then use a different one for the definition. Multiple definitions shouldn't compile; multiple declarations however is just fine.

The real problem: When you forward declare, you do NOT want to care whether it was a struct, class or union: In that context, that is an irrelevant implementation detail, that could even change later. So this warning is an artificial code smell. Still, you have to choose, and considering that class foo; has come to be the way to forward declare , I would go as far as to say the code is perfect .

The author didn't forget to update the forward declaration; he isn't supposed to (whether he changed the implementation or not, he probably didn't).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM