473,408 Members | 1,874 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,408 software developers and data experts.

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

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
6 1826

"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
[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

"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
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
Thanks everyone! I moved COPCClassFactory out of COPCServer, as
suggested.

Thanks!

Jun 21 '06 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
3
by: nicver | last post by:
I am fixing a client's Web site and for some reason an ASP class does not want to use the variables it retrieves when it initialiases. This is an excerpt of the class and it is enough to show...
3
by: charliewest | last post by:
this is a very newbie question re: oop - pls excuse my ignorance: i have inserted in each of my asp.net web forms written using C# error handing code that i would like to encapsulate in one,...
5
by: Thorbjørn Jørgensen | last post by:
Hi How can I encapsulate a specific method from a specific object? I have some different object with different methods, and I would like that the object could put a reference to one of its methods...
4
by: Miro | last post by:
Vb2003, im still learning vb.net but I do not understand my output from this logic. If someone can help me out here. Cor Ligthert, you I believe were on the right track of what Im trying to...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
4
by: nw | last post by:
Hi All, I currently have a vector of objects (lets call them MyObject). I want to perform various operations regularly on the whole vector, for example find the maximum, average, or operations...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.