1

Many questions talks about POD; But all questions talks about full object copy. Can I apply the same concept on the plain old data part of a class. Example:

struct Parent1
{
    int x;
    float y;
};

struct Parent2
{
    int k;
    float l;
};

struct NotPod : public Parent1, public Parent2
{
    char z;
    short w;
};

NotPod a, b;

void func()
{
    a.z = '4';
    a.w = 345;
    memcpy((char*)&b.z, (char*)&a.z, (char*)(&a.w)-(&a.z) + sizeof(a.w));
}

I am asking about old c++ (not C++11).

Jonas
  • 121,568
  • 97
  • 310
  • 388
Yousf
  • 3,957
  • 3
  • 27
  • 37
  • That's a pretty awful way of writing `b.z = a.z;`. – Thomas Mar 17 '13 at 16:23
  • @Thomas, not actually it is b.z = a.z; b.w = a.w; :) . And by the way this is just for sake of simplifying the question. Actually I am writing remote procedure call library. – Yousf Mar 17 '13 at 16:46
  • 1
    In what way is `NotPod` not POD? – Peter Wood Mar 20 '13 at 13:35
  • NotPod is not POD because it defines members and parent defined members. Anyway, I will modify the code to make it more visible. – Yousf Mar 20 '13 at 16:06
  • 1
    That doesn't make it not POD. See [`is_pod`](http://en.cppreference.com/w/cpp/types/is_pod), [`is_trivial`](http://en.cppreference.com/w/cpp/types/is_trivial), and [`is_standard_layout`](http://en.cppreference.com/w/cpp/types/is_standard_layout). – Peter Wood Mar 20 '13 at 16:12
  • It is not POD, refer to [What are Aggregates and PODs and how/why are they special?](http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special). It violated rule 5 in is_standard_layout page. BTW, These new functions i_pod, is_trivial and is_standard_layout are C++11 which changes POD meaning slightly. – Yousf Mar 20 '13 at 16:39
  • Okay, I see your edit has introduced multiple inheritance with non-static members. This changes things substantially. – Peter Wood Mar 21 '13 at 08:51

3 Answers3

2

As I understand from your sample code, your question isn't about PODs. What you need is a guarantee that members of your most derived class are aggregates and they have continuous memory layout.

See 9.2.12 (ISO 14882:2003)

Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1).

Simply speaking, don't put public/protected/private access specifiers in between of your sequence of aggregates and you'll get such a guarantee.

Carsten
  • 11,287
  • 7
  • 39
  • 62
0

So you only want to copy the members of the Parent part in the struct. If yes, why not implementing like in func3()?

void func2()
{
    a.z = '7';
    a.w = 444;
    a.x = 4;
    a.y = 2.1;
    memcpy(&b, &a, sizeof(NotPod)); // copies all the members
}

void func3()
{  
    a.z = '7';
    a.w = 444;
    a.x = 4;
    a.y = 2.1;
    memcpy(&b, &a, sizeof(Parent)); // copies only the members of the Parent
}
Martin Komischke
  • 1,440
  • 1
  • 13
  • 24
  • I doubt that this is guaranteed. You are copying non-pod, which is implementation dependent. – Yousf Mar 18 '13 at 09:10
0

As you said yourself its not a POD in C++03. Parts of it are (Parent1 and Parent2), but you want to treat the rest as a POD. Well, just make the rest a POD. Either by defining a local struct

struct NotPod : public Parent1, public Parent2
{
    struct InternalPod
    {
       char z;
       short w;
    };
    InternalPod i; 
};

or by inheriting from a third struct:

struct InternalPod
{
   char z;
   short w;
};

struct NotPod : public Parent1, public Parent2, public InternalPod
{
};
Rumburak
  • 3,416
  • 16
  • 27