Inheritance problems

This is a discussion on Inheritance problems within the C++ Programming forums, part of the General Programming Boards category; Hello, I am playing with inheritance, but I have ran into a problem. You see, I have a base class ...

  1. #1
    alkis_y3k
    Guest

    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!

  2. #2
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    How about using "static"?
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  3. #3
    alkis_y3k
    Guest
    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.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.
    Last edited by PJYelton; 01-03-2003 at 10:29 AM.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >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.

  6. #6
    alkis_y3k
    Guest
    Doh, it was working from the beggining, it was my own mistake.

    Thanks anyway!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 12:26 AM
  2. Class inheritance problems
    By Carolina in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2006, 04:30 AM
  3. inheritance problems
    By generic83 in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 05:04 PM
  4. Inheritance problems
    By wheels165 in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 03:49 PM
  5. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21