Hongyu wrote:
Hi,
I am a newbie in C++. I saw a code like the below and don't understand
it.
class A
{
public:
A();
~A();
Get();
class B GetIt();
class B GetAnother();
}
;
>
My question here is: what does the class keyword in front of B mean?
It means 'B' is a class.
B
is another class, but why it put a class keyword in front of it?
Has it been already defined?
I
would assum that we can simply use "B GetIt()". Is the class keyword
mandantory?
No. If 'B' has been defined, 'class' is superfluous, *unless* there is
another 'B' that means something else, like a value:
class B {};
int B = 42;
class A
{
...
class B GetIt(); // no confusion with 'B' object
};
If 'B' has not been defined as a class before the compiler gets to the
'GetIt' or 'GetAnother' function declarations, it will complain about
that. Here the keyword 'class' tells the compiler that 'B' is a class
without defining it fully. The alternative to this approach is what's
known as "a forward declaration":
class B;
class A
{
...
B GetIt();
B GetAnother();
};
....
class B { ...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask