Use (encapsulate) a class inside a class...

T

TBass

Hi,

I've been using C for years, but I'm new to C++. For the most part it's
been smooth, but here's my current snag:

COPCClassFactor.h
===============

class COPCClassFactory
{

int someint;
....

};

COPCServer.h
==========

class COPCClassFactory;

class COPCServer
{

COPCClassFactory * pCF;
....

};


myapp.cpp
========

#include "COPCServer.h"

main()
{

COPCServer kt;

/* at this point, I want to get back to someint */
/* and change its value */
/* and I'm a little unsure as how to do that. */

/* my current attempt leaves some tasks undone */
kt.pCF->someint = 5;
/* compiler error: use of undefined type COPCClassFactory */

/* but it's just a pointer to COPCClassFactory, */
/* I still need to initalize one */
/* but myapp.cpp doesn't "see" COPCClassFactory. Doing: */

kt.pCF = new COPCClassFactory;
/* compiler error: no appropriate default constructor */

/* Do that in constructor of COPCServer class? */


}


I knew what I was typing was wrong; I'm just not sure of the proper
method. Can anyone point me in the right direction?

Thanks in advance,
TBJ
 
H

Howard

TBass said:
Hi,

I've been using C for years, but I'm new to C++. For the most part it's
been smooth, but here's my current snag:

COPCClassFactor.h
===============

class COPCClassFactory
{

int someint;
....

};

COPCServer.h
==========

class COPCClassFactory;

class COPCServer
{

COPCClassFactory * pCF;
....

};


myapp.cpp
========

#include "COPCServer.h"

main()
{

COPCServer kt;

/* at this point, I want to get back to someint */
/* and change its value */
/* and I'm a little unsure as how to do that. */

/* my current attempt leaves some tasks undone */
kt.pCF->someint = 5;
/* compiler error: use of undefined type COPCClassFactory */

You didn't include COPCClassFactor.h in myapp.cpp. You need to include the
header file in order for your compiler to know where to look for that class
definition.
/* but it's just a pointer to COPCClassFactory, */
/* I still need to initalize one */
/* but myapp.cpp doesn't "see" COPCClassFactory. Doing: */

kt.pCF = new COPCClassFactory;
/* compiler error: no appropriate default constructor */

/* Do that in constructor of COPCServer class? */

Yes, that's where one usually constructs members. Your terminology ("class
factory") leads me to believe your aim is something more complicated than
this simple code, though, but without knowing what you're trying to
accomplish by all this, I can't tell if you're doing it right or not.

-Howard
 
T

TBass

[snip]
You didn't include COPCClassFactor.h in myapp.cpp. You need to include the
header file in order for your compiler to know where to look for that class
definition.
[/snip]

My goal is to only need to include the one file. Is there any way to do
that?

Thanks!
TBJ
 
H

Howard

TBass said:
[snip]
You didn't include COPCClassFactor.h in myapp.cpp. You need to include
the
header file in order for your compiler to know where to look for that
class
definition.
[/snip]

My goal is to only need to include the one file. Is there any way to do
that?

Sure. Just never refer directly to the pCF member itself, from myapp.cpp.
Instead, do anything which needs to use that member in public member
functions of the server class. Like, say, getInt(), or setInt(int), (or
even operator overloads).

-Howard
 
M

mlimber

TBass said:
[snip]
You didn't include COPCClassFactor.h in myapp.cpp. You need to include the
header file in order for your compiler to know where to look for that class
definition.
[/snip]

My goal is to only need to include the one file. Is there any way to do
that?

Why is that your goal? See this article, which outlines the proper
goals for including minimal header files and reducing dependencies:

http://www.gotw.ca/publications/mill04.htm

Also, you generally shouldn't use public data, but rather, you should
(as Howard suggests) have the member functions of COPCServer hide the
details. See this FAQ:

http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.4

Cheers! --M
 
M

mlimber

TBass said:
Thanks everyone! I moved COPCClassFactory out of COPCServer, as
suggested.

Uhh, neither of us suggested that (though it might be a good alternate
solution). We suggested that, if the factory should be a member of the
server according to your design, you should hide the details of its
creation, deletion, and usage behind the public interface functions of
COPCServer.

Cheers! --M
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top