468,768 Members | 1,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

What does class keyword in a Class mean?

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? B
is another class, but why it put a class keyword in front of it? I
would assum that we can simply use "B GetIt()". Is the class keyword
mandantory?

Thanks for answering my question in advance.

Hongyu
Aug 21 '08 #1
3 1764
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
Aug 21 '08 #2
Victor Bazarov wrote:
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
Or just declared...
>, '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
declared :-)
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 { ...
Which I'd definitely recommend (do you see any reasons for going with
the first approach?)

For the OP:
another possible reason for the "class B" is that the author of the
code is a C++ newbie passing from C to C++, used to write struct B all
over the places and assuming that the C++ analogous, "class B", is
necessary whenever struct B would be necessary in C.

struct B;

/*
Unless you introduce other declarations for the name
B (typically a typedef), the C language requires the
struct keyword here
*/
struct B GetIt();
struct B GetAnother();

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Aug 21 '08 #3
On Aug 21, 6:01*pm, Gennaro Prota <gennaro/pr...@yahoo.comwrote:
Victor Bazarov wrote:
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

Or just declared...
, '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

declared :-)


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 { ...

Which I'd definitely recommend (do you see any reasons for going with
the first approach?)

For the OP:
another possible reason for the "class B" is that the author of the
code is a C++ newbie passing from C to C++, used to write struct B all
over the places and assuming that the C++ analogous, "class B", is
necessary whenever struct B would be necessary in C.

* *struct B;

* */*
* * * Unless you introduce other declarations for the name
* * * B (typically a typedef), the C language requires the
* * * struct keyword here
* **/
* *struct B GetIt();
* *struct B GetAnother();

--
* *Gennaro Prota * * * * | * * * * * name.surname yahoo.com
* * *Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
* * *Do you need expertise in C++? * I'm available.- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
I see. Thanks Victor and Gennaro's explaination.
Aug 22 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Giggle Girl | last post: by
21 posts views Thread by Niu Xiao | last post: by
13 posts views Thread by Herman Jones | last post: by
4 posts views Thread by grizggg | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.