Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing class and struct

I'm well aware of the difference between class and struct, however I'm struggling to authoritatively say if this is well defined:

// declare foo (struct)
struct foo;

// define foo (class)
class foo {
};

// instance of foo, claiming to be a struct again! Well defined?
struct foo bar;

// mixing class and struct like this upsets at least one compiler (names are mangled differently)
const foo& test() {
   return bar;
}

int main() {
   test();
   return 0;
}

If this is undefined behaviour can someone point me in the direction of an authoritative (i.e. chapter and verse from ISO) reference?

The compiler with problems handling this (Carbide 2.7) is relatively old and all the other compilers I've tried it on are perfectly happy with this, but clearly that doesn't prove anything.

My intuition was this ought to be undefined behaviour but I can't find anything to confirm this and I'm surprised that none of the GCC versions or Comeau so much as warned about it.

like image 761
Flexo Avatar asked Feb 01 '11 18:02

Flexo


People also ask

Can you put a struct in a class?

Yes you can. In c++, class and struct are kind of similar. We can define not only structure inside a class, but also a class inside one.

Is it better to use struct or class?

Class instances each have an identity and are passed by reference, while structs are handled and mutated as values. Basically, if we want all of the changes that are made to a given object to be applied the same instance, then we should use a class — otherwise a struct will most likely be a more appropriate choice.

Can a class inherit a struct?

Yes, struct can inherit from class in C++. In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.

Is struct faster than class C++?

On runtime level there is no difference between structs and classes in C++ at all. So it doesn't make any performance difference whether you use struct A or class A in your code.


2 Answers

It looks to me like it's defined behavior. In particular, §9.1/2 says:

A declaration consisting solely of class-key identifier ; is either a redeclaration of the name in the current scope or a forward declaration of the identifier as a class name. It introduces the class name into the current scope.

The standard distinguishes between using class, struct or union when defining a class, but here, talking about a declaration, no such distinction is made -- using one class-key is equivalent to any other.

like image 74
Jerry Coffin Avatar answered Oct 11 '22 12:10

Jerry Coffin


Technically the code is ok, according to the language standard. However, as at least one of the most popular compilers issues a warning for this, it doesn't work in practice.

"In theory, there is no difference between theory and practice. In pratice, there is."

like image 39
Bo Persson Avatar answered Oct 11 '22 13:10

Bo Persson