I'm not sure what you mean, do you want one unique structure that they all use or do you want each child class to have their own unique structure that it can only update?
If you want the second thing, then at least for me it works fine:
Code:
#include<iostream>
using namespace std;
struct struct1
{
int x,y,z;
};
class parent
{
public:
parent() {};
~parent() {};
protected:
struct1 st;
};
class child : public parent
{
public:
child() {};
~child() {};
void Dummy1(int, int, int); //this accesses some variables.
void display();
};
void child::Dummy1(int x, int y, int z)
{
st.x=x;
st.y=y;
st.z=z;
}
void child::display()
{
cout<<st.x<<" "<<st.y<<" "<<st.z<<endl;
}
int main()
{
child c1;
child c2;
c1.Dummy1(1,2,3);
c2.Dummy1(2,3,4);
c1.display(); // prints 1 2 3
c2.display(); // prints 2 3 4
return 0;
}
If you want only one unique structure that they can all use, then I think you need to make it static. But make sure you declare the values outside of the class like so:
Code:
#include<iostream>
using namespace std;
struct struct1
{
struct1(int X, int Y, int Z) : x(X), y(Y), z(Z) {};
int x,y,z;
};
class parent
{
public:
parent() {};
~parent() {};
protected:
static struct1 st;
};
struct1 parent::st(0,0,0);
class child : public parent
{
public:
child() {};
~child() {};
void Dummy1(int, int, int); //this accesses some variables.
void display();
};
void child::Dummy1(int x, int y, int z)
{
st.x=x;
st.y=y;
st.z=z;
}
void child::display()
{
cout<<st.x<<" "<<st.y<<" "<<st.z<<endl;
}
int main()
{
child c1;
child c2;
c1.Dummy1(1,2,3);
c2.Dummy1(2,3,4);
c1.display(); // prints 2 3 4
c2.display(); // prints 2 3 4
return 0;
}
Since all static variables including structs and classes need to be initialized.