?
Solved

Class Initialization?

Posted on 2007-10-16
5
Medium Priority
?
211 Views
Last Modified: 2010-04-01
I am given the a Person.h file which describes a Person.  At the top of the file there is a class initialized like so:

#include <string>

using namespace std;

class Job;

class Person {...}

Why does the line "class Job;" appear at the top?
0
Comment
Question by:323k13l
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
ikework earned 200 total points
ID: 20087359
it is a forward declaration. it tells the compiler, that a class Job exists and it will be declared later.
this way you can use a pointer to class Job and a reference to it inside the header without actually include the header-file where the class Job is declared ..

ike
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20087388
Hi 323k13l,

This is called a "forward reference".  Somewhere in the class Person, there is probably a reference to a class called Job.  If class Job isn't declared, using it in class Person is a compilation error.

This technique allows you to declare a class (Job) and use it in another class (Person).  Then also use the second class (Person) in the first class (Job).

class Job;

class Person
{
  private Job MyJob;
};

class Job
{
  private Person Me;
};


Good Luck,
Kent
0
 
LVL 20

Expert Comment

by:ikework
ID: 20087414
>> This technique allows you to declare a class (Job) and use it in another class (Person).  Then also use the second class (Person) in the first class (Job).

hi kent .. it only allows to use pointers or references, but not objects as in your example

class Job;

class Person
{
    void test( Job &j ) {}  // << reference is legal

    Job *pMyJob; // << pointer is legal

    Job MyJob;  // objects are NOT legal ..
};


ike
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20087462
Hi ikework,

Yeah.  i'd just gotten back from lunch and saw that the question was unanswered so I threw one together and didn't proofread it before posting.


Thanks for cleanning up,
Kent
0
 
LVL 20

Expert Comment

by:ikework
ID: 20087493
@kent .. c'est la vie ..  :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question