Click to See Complete Forum and Search --> : syntax question


sohayla
July 30th, 1999, 07:44 PM
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

ChrisD
July 30th, 1999, 08:58 PM
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

aamirdogar
July 31st, 1999, 01:30 AM
the public infront of the class name is for the inheritence one class from the other one.
anything else?

Victor Boctor
August 2nd, 1999, 01:59 PM
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!