any link and binary level difference between c++ struct and class?

K

kjin101

1. will a given C++ compiler always generate same mangled name for
class A and struct A, and same mangled signature where class A is
replaced with struct A?
2. will a given C++ compiler always generate same memory layout for
class A and struct A that have same member order, data types, and
parent hierarchy declarations.

Appreciate your answers!
Ke
 
Y

year1943

On 10 , 22:43, (e-mail address removed) wrote:
As I know, struct and class act like synonims, except default member
access/visibility status: public for struct, private for class...
So struct { ... } should be equivalent to class { public: ... }
 
K

kjin101

On 10 , 22:43, (e-mail address removed) wrote:
As I know, struct and class act like synonims, except default member
access/visibility status: public for struct, private for class...
So struct { ... } should be equivalent to class { public: ... }

Yes, at source code level, they are synonims. I am keen to know
whether this could also be safely assumed at link and binary level.

For instance, in one binary file, we have:

class A : some parents {
// some members
};

A* foo() {
return new A;
}

in another binary file (built with the exactly same compiler), we
have:

struct A : same parents {
// same member order/type declaration
};

void bar() {
A* a = foo(); // assumed mangled signatures are same

a->blabla(); // assumed memory layouts are same
}

Are these two object files (or libraries) able to be linked together
and work properly across all C++ compilers?
 
Y

year1943

Yes, at source code level, they are synonims. I am keen to know
whether this could also be safely assumed at link and binary level.
Standard tells like 9(.0).4: "A structure is a class defined with the
class-key struct; its members and base classes are public by default."
I see nothing interesting on layout diffs or equality for struct; for
unions there are such notes, for struct I see no.
 
J

James Kanze

1. will a given C++ compiler always generate same mangled name for
class A and struct A, and same mangled signature where class A is
replaced with struct A?

Does an implementation mangle names? Whether and how are
implementation details, and not defined by the language.

What is required is that if you write:

class C ;
void f( C& ) ;

in one file, and you define:

struct C { /*...*/ } ;
void f( C& c ) { /* ... */ }

in another, the f() in the first file refers to the function
defined in the second. How the compiler does this is its
business.
2. will a given C++ compiler always generate same memory layout for
class A and struct A that have same member order, data types, and
parent hierarchy declarations.

Probably. But be careful. If you *define* the type using
struct in one file, and using class in another, you have
undefined behavior (although I can't imagine an implementation
where it really caused problems). The different use can only
concern class declarations, e.g.:

class C ;
struct C { int a ; int b; } ;

(and that is legal even if both declarations are in the same
file).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top