472,958 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 1956
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
59
by: Chris Dunaway | last post by:
The C# 3.0 spec (http://msdn.microsoft.com/vcsharp/future/) contains a feature called "Implicitly typed local variables". The type of the variable is determined at compile time based on the...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
4
by: Giggle Girl | last post by:
I have inherited a stylesheet at work, with the designer no longer working here. In an external stylesheet it says: thead th, thead th.locked { position:relative; }
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
4
by: huguogang | last post by:
Just curious, any one know what the 3 part parameter "class CString filename" would mean. The code: int TestFunc(class CString filename) { fopen(filename, "w"); } Compile using Visaul C++,...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
13
by: Herman Jones | last post by:
I found this statement in some sample code. It seems to be syntactically correct. What do the brackets around "String" mean? Dim sWork As = "Some number of characters"
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.