0

Currently, I am trying to experiment with the uses of nested classes and inheritance within in these nested classes. The experiment I am running is shown below. In the code below, I have a class Outside and within Outside there is a class called Father. Additionally, I created two classes, Child A and Child B where they are derived from Father. I numbered the issues and concerns that I am facing below:

  1. In the class Outside, I notice that I get an error when I try to create the member variables dad, son, and daughter with Father dad, Child A and Child B, respectively, within Outside. Namely, I get an error saying incomplete datatype is not allowed. Judging from this, it looks the program was not able to see the constructor or the instructions of those objects. In some examples, namely this video https://www.youtube.com/watch?v=C5X7h6h_tks at 2:06, the nested object can be a member variable within Outside. Why is this method legal and why is my method not? If I simply write:

    class Father;
    class ClassA;
    class ClassB;
    

    Wouldn't that indicate to the compiler that Father, ChildA, ChildB exists inside Outside and that I should be able to create a member variable of these classes within Outside?

  2. Also, within the member function, awesome, in class Outside, I am able to declare a variable Father, but not ChildA and ChildB. Why is that? Is there even a way to declare ChildA and ChildB variables within awesome or in the declaration of Outside.

  3. Talking about the member function awesome again, I notice that I cannot do

      Father* ptr; 
      ptr = &dummyB; 
      //Error: A value of type "Outside::ChildA *" cannot be assigned an entity of type
    

    However, if I do it in the main function, I do no get an error. Why is that? Additionally, I get a similar error when I try

    dad_ptr = a_ptr;
    

    where dad_ptr and a_ptr are member variables of Outside. Is there a way to change the assignment of a pointer to an object to its child. My main objective in this experiment is Change the assignment of a pointer of nested object within the object that it is enclosed.

  4. Within the member function something_crazy in the class ChildB, the following is illegal:

    dad_ptr = a_ptr;
    

    Since this class is derived from Father, which is within Outside, shouldn't it have access to the member function and variables of Outside as well? How can I edit my code so that these classes can have access to Outside. Even if they do have access to the member functions, is this even possible to do. If not why?

    #include <iostream>
    #include <string>
    using namespace std;
    class Outside{
    
        public:
            int god;
        class Father;
        class ChildA;
        class ChildB;
        Outside(){
            //dad_ptr = new Father;
            cout << "Outside is envoked";
        }
        void run(){
            //ChildA lol;
        }
        void awesome();
    
    public:
    
        Father* dad_ptr;
        ChildA* a_ptr;
        ChildB* b_ptr;
    
        Father dad;// Error: incomplete datatype not allowed;
        ChildA son;//Error:incomplete datatype not allowed;
        ChildB daughter; //Error:incomplete datatype not allowed;
    
    
    public:
    
    };
    
    
    class Outside::Father{
    public:
        Father(){
            data = "Dad";
    
        }
        string data;
        virtual void something(){
            cout << "Father said something stupid";
        }
    
    
    };
    
    
    
    class ChildA : public Outside::Father{
    public:
        ChildA() {
            data = "Child A";
        }
        void something(){
            cout << "Child A said something stupid";
        }
    
    
    };
    
    class ChildB : public Outside::Father{
    public:
        ChildB() {
            data = "Child B";
        }
        void something(){
            cout << "Child B said something stupid";
        }
    
        void something_crazy(){
            dad_ptr = a_ptr; //Since Father is a nested of Outside, shouldn't a derive class have access to the objects of Outside.
        }
    
    };
    
    
    void Outside::awesome(){
        Father something; //Why can I create a variable father inside a member function, but cannot create it in the declaration of the class? Why can I create father, but not son or daughter?
        /*This cannot be done at all*/
        ChildA dummyA;
        ChildB dummyB;
        Father* ptr;
        ptr = &dummyB;
    
    
        //What I really want.
        dad_ptr = a_ptr; //Error: A value of type "Outside::ChildA *" cannot be assigned an entity of type "Outside::Father*";
    
    }
    
    int main(){
        ChildA dummyA;
        ChildB dummyB;
        Outside::Father* dad_pointer;
        ChildA* another_ptr;
        another_ptr = &dummyA;
        ptr = another_ptr;
        //ptrB = &dummyA;
        ptr = &dummyA;
        ptr->something_stupid();
        cout << endl;
        ptr = &dummyB;
        ptr->something_stupid();
        return 0;
    }
    
mtber75
  • 938
  • 1
  • 8
  • 15
  • 1
    Try to keep your post to 1 question at a time, otherwise they end up too broad. – Cory Kramer Nov 12 '14 at 17:31
  • Most of your questions have a common theme: You are forward declaring the classes (resulting in incomplete types). This allows you to use pointers and references to those classes, but not instances. See this post http://stackoverflow.com/questions/3632818/forward-declaration-vs-include – Cory Kramer Nov 12 '14 at 17:34
  • 1
    Just keep the complete nested class declaration inside 'outside' –  Nov 12 '14 at 17:37
  • If you have some class `B` declared in another class `A`, you should access to `A`'s namespace to instantiate it outside from `A`: `A::B b;` Your types are incomplete because there are no such types outside from `Outside` class. –  Nov 12 '14 at 17:49
  • Wow, so it looks like for the most part it looks like if I put these classes inside "Outside" it works. But if I put all of these definitions inside "Outside", it looks extremely slopy. Is it possible to put each class in their own respective header file and cpp file. – mtber75 Nov 12 '14 at 19:31
  • Additionally, for 4, ChildA and ChildB can see the member variables of Outside, but I still get an error in the function something_crazy in the class ChildB. The error is nonstatic member reference must be relative to a specific error. How can I solve this problem? – mtber75 Nov 12 '14 at 19:34

0 Answers0