-
Inheritance problems
Hello, I am playing with inheritance, but I have ran into a problem. You see, I have a base class that another class inherits from. In the base class, I havea structure, including some floats. The child class can access them.
Code:
class parent
{
public:
parent();
~parent();
protected:
struct1 st;
};
class child : public parent
{
public:
child();
~child();
void Dummy1(); //this accesses some variables.
};
However the problem is that there is no unique parent class instance, so the struct1 is always updated with the last call Dummy1 i call. If i have 2 child classes declared like this:
Code:
child c1;
child c2;
Then the st structure will hold the values I set last. In other words, st is not unique.. Anyone can solve this problem?
Thanks a lot!
-
How about using "static"?
-
unresolved external symbol "protected: static struct rtMeshStruct CRTStaticBaseMesh::meshes" (?meshes@CRTStaticBaseMesh@@1UrtMeshStruct@@A)
rStaticMesh.obj : error LNK2001: unresolved external symbol "protected: static struct rtMeshStruct CRTStaticBaseMesh::meshes" (?meshes@CRTStaticBaseMesh@@1UrtMeshStruct@@A)
This is the error when I change the struct declaration to static.
-
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.
-
>This is the error when I change the struct declaration to static.
A static datamember can be seen as a global variable which scope is restricted to the class.
When you have added a static variable to a class, you still have to define and eventually initialise it. So if you for example add a variabele flag to the class like this:
static bool flag;
Then you need to define and eventually initialise it outside the class, just as you would do with normal global variables. Since the scope is restricted to the class, you need to apply the scope operator. Such like this:
bool myclass::flag = false;
You have probably only put static in front of it. Therefore the compiler complains about the unresolved external symbol.
-
Doh, it was working from the beggining, it was my own mistake.
Thanks anyway!