Forward Declaration of Class, Function

前端 未结 6 1044
夕颜
夕颜 2021-01-04 19:26

When forward declarations of functions work in a source file (.cpp), why would the same doesn\'t work for classes ?

Thanks.

// main.cpp

void forwar         


        
相关标签:
6条回答
  • 2021-01-04 19:41

    when you create 2 class & one function can access data from on class to another class then it is a friend function

    forword declaration is use to know which class in next

    class abc;

    class xyz

    {

    data member;

    public:

    friend void getdata();

    other member function

    }

    class abc

    {

    data member

    public:

    friend void getdata();

    }

    0 讨论(0)
  • 2021-01-04 19:52

    Forward declaration can work for classes too:

    class Foo;
    
    class Bar {
    public:
        Foo *myFoo; // This has to be a pointer, thanks for catching this!
    };
    
    class Foo {
    public:
        int value;
    };
    

    The above code shows a forward declaration of the Foo class, using a variable of type Foo* in another class (Bar), then the actual definition of the Foo class. C++ doesn't care if you leave things unimplemented as long as you implement them before using its code. Defining pointers to objects of a certain type is not "using its code."

    Quick, dirty reply but I hope it helps.

    Edit: Declaring a non-pointer variable of a class thats unimplemented will NOT compile as the replies stated out. Doing so is exactly what I meant by "using its code." In this case, the Foo constructor would be called whenever the Bar constructor is called, given that it has a member variable of type Foo. Since the compiler doesn't know that you plan on implementing Foo later on, it will throw an error. Sorry for my mistake ;).

    0 讨论(0)
  • 2021-01-04 19:53

    The forward declaration class One; allows you to refer to the class itself but not to any of its members. You have to put all definitions of class members after the full declaration of the class. (Or inside, of course.)

    0 讨论(0)
  • 2021-01-04 19:57

    The compiler reads stuff from beginning to end, and generates code as it goes. (Some compilers may not do this, but they should behave as if they did.) But before the class is defined, the compiler doesn't know that One::statVar or One::anyAccess should exist, or whether the function is virtual, static, or what. It needs to know that stuff in order to generate code.

    0 讨论(0)
  • 2021-01-04 20:02

    Place your member declaration of your class before the member implementations.

    class One {
    
     public:
      void anyAccess() ;
      static int statVar ;
    
     private:
      int  classVar ;
    
    } ;
    
    int One:: statVar = 10 ;
    
    void
    One :: anyAccess() {
    
     std::cout << "\n statVar:\t " << statVar ;
     std::cout << "\n classVar:\t" << classVar ;
    }
    
    0 讨论(0)
  • 2021-01-04 20:03

    You're getting the error message on int One:: statVar = 10 ; NOT on the forward declaration, which is fine.

    The compiler needs to know the full definition of the class before you can define static members like that - a forward declaration is insufficient (it needs to be able to confirm that the type is correct from the class definition).

    You'll need to move your static attribute definition below the class definition.

    0 讨论(0)
提交回复
热议问题