Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Circular reference

36 views
Skip to first unread message

Joseph Hesse

unread,
May 4, 2015, 8:58:52 AM5/4/15
to
Suppose I have two classes, class Person and class City. A City class
is basically a set of Persons. An outline of the code would be:

class Person
{
private:
City *city; // the city in which the person lives
// ...
public:
// ...
};

class City
{
private:
std::set<person> citizens;
// ...
public:
// ...
};

How do I get "City *city;" in class Person to work since the declaration
of City occurs after the declaration of Person?

Thank you,
Joe

Joseph Hesse

unread,
May 4, 2015, 8:59:54 AM5/4/15
to
Suppose I have two classes, class Person and class City. A City class
is basically a set of Persons. An outline of the code would be:

class Person
{
private:
City *city; // the city in which the person lives
// ...
public:
// ...
};

class City
{
private:
std::set<Person> citizens;
Message has been deleted

Joseph Hesse

unread,
May 4, 2015, 9:19:03 AM5/4/15
to
On 05/04/2015 08:03 AM, Stefan Ram wrote:
> Joseph Hesse <jo...@gmail.com> writes:
>> How do I get "City *city;" in class Person to work since the declaration
>> of City occurs after the declaration of Person?
>
> class City;
> class Person{ City * city; };
> class City {};
> int main(){}
>
Thank you. One further question.
To introduce "class City" before "class Person", is it better style to
explicitly write "class City" as in the above code or to include the
header file for City?

Victor Bazarov

unread,
May 4, 2015, 9:35:00 AM5/4/15
to
Including a header file is just text substitution, essentially. Think
what that means. What contents does your header have? What Stefan
showed here is what the compiler will accept. Split it into headers and
translation units that include those headers as you please.

V
--
I do not respond to top-posted replies, please don't ask

Jorgen Grahn

unread,
May 4, 2015, 10:53:43 AM5/4/15
to
If I have the choice, I prefer just declaring the class (City), to
including city.h. Especially if City is just an implementation detail
of Person, which the user of Person isn't always interested in.

I don't know which style is better, but there's nothing wrong with
forward-declarations and one should not avoid using them.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Richard

unread,
May 4, 2015, 1:21:23 PM5/4/15
to
[Please do not mail me a copy of your followup]

Joseph Hesse <jo...@gmail.com> spake the secret code
<XradnbRqubkg7NrI...@giganews.com> thusly:
Always prefer forward declarations over including a header to avoid
unnecessarily lengthy compile cycles.

Modules will fix this, I think. But until we have modules, prefer
forward declarations over including additional headers.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Dombo

unread,
May 4, 2015, 3:02:14 PM5/4/15
to
Op 04-May-15 14:59, Joseph Hesse schreef:
You need a forward declaration of City so the compiler knows that "City"
is a class:

class City;

class Person
{
private:
City *city; // the city in which the person lives
// ...
public:
// ...
};

class City
{
private:
std::set<Person> citizens;
// ...
public:
// ...
};

Note that a forward declaration suffices for the Person class because
the city member is a pointer. If the city member was an instance of the
class City it wouldn't compile because at that point the compiler
doesn't know yet how big instances of the City class will be and
therefore cannot know how big instances of the Person class would be.


0 new messages