CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: syntax question

  1. #1
    Join Date
    Jul 1999
    Posts
    8

    syntax question

    I saw something in one of the MSDN samples that I haven't seen before and would like some explanation.
    What exactly does it mean when you write the statement " class CMyclass; " before the definition of another class. For example,

    class CDrawView;

    class CDrawDoc : public COleDocument
    {
    ...........
    }

    Thanks for you help


  2. #2
    Join Date
    May 1999
    Posts
    667

    Re: syntax question

    It's called a forward declaration, it tells the compiler that CDrawView is a class name when it encounters it. YOu can only use forward declarations when dealing with pointers to the declared class, anything more than that the compier need more info.

    ex:
    // This is an acceptable forward declaration
    class a;
    class b
    {
    public:
    b(a* aIn);

    a* getClassA();

    private:
    a* m_a;
    };

    //The forward delaration is not enough for the compiler here because it needs
    //to know the size of class a to generate this. So you need to include the
    //header file that contains class a

    class b
    {
    public:
    b(a aIn);

    a* getClassA();

    private:
    a m_a;
    };

    HTH,
    Chrs



  3. #3
    Join Date
    Jun 1999
    Location
    Pakistan
    Posts
    77

    Re: syntax question

    the public infront of the class name is for the inheritence one class from the other one.
    anything else?


  4. #4

    The reason behind its use!

    This is correct. I just wanted to add that this technique has two uses:

    1. Do not include the header file of this class which minimized re-compilation dependencies and accordingly compilation time.

    2. This technique is also useful in case of circler reference. Where class A has a pointer to class B and class B has a pointer to class A.

    I hope this makes things clearer!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured