What does class keyword in a Class mean?

H

Hongyu

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
 
G

Gennaro Prota

Victor said:
It means 'B' is a class.


Has it been already defined?


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();
 
H

Hongyu

Or just declared...





declared :)









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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top