473,396 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1974
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.