-1

I have 3 separate header files. Class A, Class B is derived A and Class.

class A {
public:
    virtual void foo(C ...object...);
};

class B : public A {
public:
    void foo(C ...object...);
};

class C {
public:
    friend class A;
    friend class B;
private:
    A *arr[num][num];
};

Arr is pointer of 2D array.The inside of the 2D array is filled with B objects.How can I access the C class object from the header file of class B? Is it possible?If possible, how should the "include .h" seperates header files and foo function prototype be?

CoralK
  • 3,341
  • 2
  • 9
  • 22
kblt
  • 3
  • 2
  • 1
    What include patterns have you tried? Did they fail? – Stephen Newell Jul 11 '20 at 00:14
  • not nested class. – kblt Jul 11 '20 at 00:15
  • 1
    have a look at [forward declarations](https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c) – Rikus Honey Jul 11 '20 at 00:18
  • @StephenNewell I have tried many combinations but it did not work. I cannot introduce the Class C object in the header file with Class B. There is include A.h and B.h in Class C for friend definition. And I get a lot of errors when I try to include C.h in the Class B or Class A header files. – kblt Jul 11 '20 at 00:21
  • Use forward declarations in `class C`. You can even use them in all 3 header files. Include the headers just in the source files. – Thomas Sablik Jul 11 '20 at 00:22
  • `class B : public A{} {` --- does `A{}` a typo? – CoralK Jul 11 '20 at 00:24
  • So post those exact errors. And start smaller, adding dependencies as you need them. Until A.h can be #included without error, don't even touch the other files. – Stephen Newell Jul 11 '20 at 00:26
  • @KorelK Sorry I edited. – kblt Jul 11 '20 at 00:31

1 Answers1

-1

A common way to split your code into separate files is following:

  • A.h

    #pragma once
    class C;
    
    class A {
    public:
        virtual void foo(C c);
    };
    

    Class A doesn't need any type information of C. The declaration of member function foo doesn't need complete type information but a declaration because the function expects a parameter of type C.

  • A.cpp

    #include "A.h"
    #include "C.h"
    
    void A::foo(C c) {}
    

    The definition of function A::foo(C c) needs complete type information of C. Therefore the class definition of C is included.

  • B.h

    #pragma once
    #include "A.h"
    
    class C;
    
    class B : public A {
    public:
         void foo(C c) override;
    };
    

    Class B needs complete type information of A for inheritance. Therefore the class definition of A is included. Complete type information of class C is not necessary (see A.h).

  • B.cpp

    #include "B.h"
    #include "C.h"
    
    void B::foo(C c) {}
    

    Same as in A.cpp

  • C.h

    #pragma once
    class A;
    
    class C {
    public:
         friend class A;
         friend class B;
    private:
         A *arr[5][5];
    };
    

    Class C just contains friend declarations of A and B and a pointer to type A. A friend declaration don't need a declaration or full type information. Since all pointers to objects have same size complete type information for class A is not necessary. It will become necessary when the pointer is dereferenced.

All source files also contain an include of the corresponding header file.

Thomas Sablik
  • 15,600
  • 7
  • 29
  • 55