By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jun 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"TBass" <tb*@automateddesign.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
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
Jun 21 '06 #2

P: n/a
[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

Jun 21 '06 #3

P: n/a

"TBass" <tb*@automateddesign.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
[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
Jun 21 '06 #4

P: n/a
TBass wrote:
[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-lit...s.html#faq-7.4

Cheers! --M

Jun 21 '06 #5

P: n/a
Thanks everyone! I moved COPCClassFactory out of COPCServer, as
suggested.

Thanks!

Jun 21 '06 #6

P: n/a
TBass wrote:
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

Jun 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.