4

As many books mentioned, the difference with C++ struct and class is the access control descriptor. Thus I am wondering if the following statement is right:

struct in C is unboxed: members in the struct are plainly located next to where the struct is allocated. But struct in C++ is a boxed type like class: members/headers are located somewhere else, and where the struct is allocated contains a pointer to the members/headers.

Is this understanding right?

And is it possible to create a unboxed type in C++, that also contains instance methods?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
qinsoon
  • 1,433
  • 2
  • 15
  • 34

5 Answers5

7

Looks like complete nonsense to me.

Members are not magically "located somewhere else", no pointers are involved, and headers have nothing to do with it whatsoever. The C++ compiler doesn't even know that headers exist!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I think some of that is referring to virtual table pointers. (Not that it makes it any more correct, just less insane) – Mooing Duck Feb 08 '12 at 00:31
  • @MooingDuck: Perhaps. (Indeed, nothing that a C `struct` contains -- i.e. data members -- can be magically whisked away to fairyland by C++ virtual dispatch.) – Lightness Races in Orbit Feb 08 '12 at 00:33
  • +1, but from what I understand, that last sentence is a *slight* exaggeration, even in the abstract; for example, I believe that compilers are allowed to add compiler-specific keywords if they provide compiler-specific nonstandard `< >` headers and a program `#include`s them. – ruakh Feb 08 '12 at 00:33
  • @ruakh: That would be the preprocessor. Well, ok, with some contextual information passed down the pipeline perhaps. – Lightness Races in Orbit Feb 08 '12 at 00:34
  • Eh? How can a preprocessor add keywords? – ruakh Feb 08 '12 at 00:35
  • @ruakh: It can't. But it can ask the compiler to add keywords (my second sentence), without the compiler requiring knowledge of headers -- remember, the compilation phase _does not see `#include` directives_. – Lightness Races in Orbit Feb 08 '12 at 00:36
  • Hmm. O.K., I suppose that makes sense. Thanks. :-) – ruakh Feb 08 '12 at 00:36
  • Oh, and come to think of it, I suppose the header-file could have `#define compiler_specific_keyword __compiler_specific_keyword`. – ruakh Feb 08 '12 at 00:38
  • Sorry about misconception here. By 'header', I mean object headers, or some similar terms that are used in C++, not header files. – qinsoon Feb 08 '12 at 03:00
  • @qinsoon: What is an "object header"? – Lightness Races in Orbit Feb 08 '12 at 10:07
3

The missing keyword in this discussion is 'POD' (Plain Old Data structure). (Boxing is related to .NET and possibly Java - though I don't recall Java terminology using the word)

A POD basically means that it can be moved around in memory just by 'blitting bits' (memcpy, memmov). There are explicit requirements in the C++ standard specifications.

C structs are always POD (plain old data), whereas C++ classes can have 'extra magic' related to (virtual) inheritance.

Look at this:

What are POD types in C++?

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
2

C, C++ and C# are all very different languages and it looks like you are trying to think of one in terms of another. Your statement about boxing doesn't make any sense in a C or a C++ context.

See also:

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
1

No it is completely incorrect. You could write a struct in C++ that is totally C compatible and compile code with it in C++ giving it a C interface.

You could then have a program written in C use the struct and call the functions from the library and there will be no issues whatsoever.

In fact many C++ libraries manipulate C data structs and interact with C.

You can, although it's not recommended, do this in your struct and it's still portable to C.

struct X
{
#ifdef __cplusplus
private:
#endif
// members variables

#ifdef __cplusplus
public:
 // methods

#endif

};

but portable if and only if none of the member functions are virtual. Not sure if this is what the article means by "boxing".

CashCow
  • 30,981
  • 5
  • 61
  • 92
0

In c++ the difference between struct and class is, like you correctly note, the implicit access control descriptor. class is private, while struct is public.

So the following:

struct A : B
{
  int x;
};

is equivalent to:

class A : public B
{
public:
  int x;
};

There are no other differences, from my understanding.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Gigi
  • 4,953
  • 24
  • 25