473,804 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#include verse class class_name

I will assume many people reading this would never create anything similar
to the example below. So let me preface this with _*IF*_ you were in a
situation where you had to chose between using #includes or forward
declaring each class in diamond.h, which would you choose? Why?

If there is something fundamentally wrong with the way I've approached the
structure of this example, I am interested to know. As for preferences and
tastes, I would really like to stay focused on the question above.

//north.cpp
#include "north.h"
namespace diamond {
North::North(){ }
North::~North() {}
};

//east.cpp
#include "east.h"
namespace diamond {
East::East() : North(){}
East::~East(){}
};

//west.cpp
#include "west.h"
namespace diamond {
West::West() : North(){}
West::~West(){}
};

//south.cpp
#include "south.h"
namespace diamond {
South::South(): East(), West(){}
South::~South() {}
};

//diamond.cpp
#include "diamond.h"
namespace diamond {
Diamond::Diamon d(){}
Diamond::~Diamo nd(){}
};

//north.h
#ifndef DIAMONDNORTH_H
#define DIAMONDNORTH_H
namespace diamond {
class North{
public:
North();
~North();
};
};
#endif

//east.h
#ifndef DIAMONDEAST_H
#define DIAMONDEAST_H
#include "north.h"
namespace diamond {
class East : virtual public North
{
public:
East();
~East();
};
};
#endif

//west.h
#ifndef DIAMONDWEST_H
#define DIAMONDWEST_H
#include "north.h"
namespace diamond {
class West : virtual public North
{
public:
West();
~West();
};
};
#endif

//south.h
#ifndef DIAMONDSOUTH_H
#define DIAMONDSOUTH_H
#include "east.h"
#include "west.h"
namespace diamond {
class South : virtual public East, virtual public West
{
public:
South();
~South();
};
};
#endif

//diamond.h
#ifndef DIAMONDDIAMOND_ H
#define DIAMONDDIAMOND_ H
namespace diamond
{
class Diamond
{
public:
Diamond();
~Diamond();
private:
East e;
North n;
West w;
South s;
};
};

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05
28 2024
Pete Vidler wrote:
This does not solve your problem. The diamond.h file still requires
those headers, so every source file that includes it will need to
include those headers first. You just shifted the problem onto the
people (and source files) that use your headers.
So I learned the hard way. As soon as I tried to use it from outside of the
implementation, I was back at square one.

[snip good advice, etc.]
What does this have to do with Java APIs?


I would really have to do some digging to determine how closely the
approaches correspond, but here is one example of API amd implementation
done in Java:
http://xml.apache.org/xerces2-j/javadocs/api/index.html
http://xml.apache.org/xerces2-j/java...es2/index.html
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #11

"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:Io******** ************@sp eakeasy.net...
Steven T. Hatton wrote:
I will assume many people reading this would never create anything similar to the example below. So let me preface this with _*IF*_ you were in a
situation where you had to chose between using #includes or forward
declaring each class in diamond.h, which would you choose? Why?
As it turns out, in this case I have to use the #includes. I'm not 100%

sure why that is. I just know it didn't compile, and gave an error saying the
class definitions were incomplete.
//diamond.h
#ifndef DIAMONDDIAMOND_ H
#define DIAMONDDIAMOND_ H
namespace diamond
{

/*didn't work*/
class East
class West
class North
class South
class Diamond
{
public:
Diamond();
~Diamond();
private:
East e;
North n;
West w;
South s;
};
};


--


You could store pointers instead of the objects themselves, and instantiate
them in your Diamond constructor. That only requires that your diamond.cpp
file include those other headers instead of including them in diamond.h.
(Of course, then you have to maintain those pointers yourself.)
-Howard

Jul 22 '05 #12

"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:Io******** ************@sp eakeasy.net...
Steven T. Hatton wrote:
I will assume many people reading this would never create anything similar to the example below. So let me preface this with _*IF*_ you were in a
situation where you had to chose between using #includes or forward
declaring each class in diamond.h, which would you choose? Why?
As it turns out, in this case I have to use the #includes. I'm not 100%

sure why that is. I just know it didn't compile, and gave an error saying the
class definitions were incomplete.
//diamond.h
#ifndef DIAMONDDIAMOND_ H
#define DIAMONDDIAMOND_ H
namespace diamond
{

/*didn't work*/
class East
class West
class North
class South
class Diamond
{
public:
Diamond();
~Diamond();
private:
East e;
North n;
West w;
South s;
};
};


--


You could store pointers instead of the objects themselves, and instantiate
them in your Diamond constructor. That only requires that your diamond.cpp
file include those other headers instead of including them in diamond.h.
(Of course, then you have to maintain those pointers yourself.)
-Howard

Jul 22 '05 #13
Howard wrote:
You could store pointers instead of the objects themselves, and
instantiate
them in your Diamond constructor. That only requires that your
diamond.cpp file include those other headers instead of including them in
diamond.h. (Of course, then you have to maintain those pointers yourself.)
-Howard


That is my prefered approach. I just did it with the value members to see
what happened. Most of the code I've written that does anything other than
dump a few lines of text uses Qt. That takes care of most of the need to
release memory without me having to do anything.

I guess I sould start working some of the problems in TC++PL(SE). Just
reading the text really isn't enough.

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #14
Howard wrote:
You could store pointers instead of the objects themselves, and
instantiate
them in your Diamond constructor. That only requires that your
diamond.cpp file include those other headers instead of including them in
diamond.h. (Of course, then you have to maintain those pointers yourself.)
-Howard


That is my prefered approach. I just did it with the value members to see
what happened. Most of the code I've written that does anything other than
dump a few lines of text uses Qt. That takes care of most of the need to
release memory without me having to do anything.

I guess I sould start working some of the problems in TC++PL(SE). Just
reading the text really isn't enough.

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #15
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message news:<Io******* *************@s peakeasy.net>.. .
Steven T. Hatton wrote:
[redacted]


Because in order to derive from a class, it must be fully defined
(note to Standards Gurus, my terminology may be off).

The compiler must know the full definition of the parent class before
it can be derived, so that it can know about any virtual base classes,
etc... as well as vtbl layout, and how much storage to allocate for
the base class.

Example:

class B;
class D : public B
{
public:
int x;
D(int);

D d;
-- end example

How does the compiler know how big to make D?
Jul 22 '05 #16
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message news:<Io******* *************@s peakeasy.net>.. .
Steven T. Hatton wrote:
[redacted]


Because in order to derive from a class, it must be fully defined
(note to Standards Gurus, my terminology may be off).

The compiler must know the full definition of the parent class before
it can be derived, so that it can know about any virtual base classes,
etc... as well as vtbl layout, and how much storage to allocate for
the base class.

Example:

class B;
class D : public B
{
public:
int x;
D(int);

D d;
-- end example

How does the compiler know how big to make D?
Jul 22 '05 #17
red floyd wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:<Io******* *************@s peakeasy.net>.. .
Steven T. Hatton wrote:
[redacted]
Because in order to derive from a class, it must be fully defined
(note to Standards Gurus, my terminology may be off).

The compiler must know the full definition of the parent class before
it can be derived, so that it can know about any virtual base classes,
etc... as well as vtbl layout, and how much storage to allocate for
the base class.

Example:

class B;
class D : public B
{
public:
int x;
D(int);

D d;
-- end example


My example wasn't using inheritance, but the the same argument probably
applies.
How does the compiler know how big to make D?


That wasn't the part I was uncertain of. It was why the implementation
wasn't available to the compiler at the time. It turns out I probably was
correct regarding the assumption that the implementation was part of the
translation unit. But it seems the forward declaration was not sufficient
to get the compiler to postpone the attempt to process the class
definition. I think the answer is probably in Clause 2 of PL-C++03 (The
Standard).
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #18
red floyd wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:<Io******* *************@s peakeasy.net>.. .
Steven T. Hatton wrote:
[redacted]
Because in order to derive from a class, it must be fully defined
(note to Standards Gurus, my terminology may be off).

The compiler must know the full definition of the parent class before
it can be derived, so that it can know about any virtual base classes,
etc... as well as vtbl layout, and how much storage to allocate for
the base class.

Example:

class B;
class D : public B
{
public:
int x;
D(int);

D d;
-- end example


My example wasn't using inheritance, but the the same argument probably
applies.
How does the compiler know how big to make D?


That wasn't the part I was uncertain of. It was why the implementation
wasn't available to the compiler at the time. It turns out I probably was
correct regarding the assumption that the implementation was part of the
translation unit. But it seems the forward declaration was not sufficient
to get the compiler to postpone the attempt to process the class
definition. I think the answer is probably in Clause 2 of PL-C++03 (The
Standard).
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #19
Steven T. Hatton wrote:
That wasn't the part I was uncertain of. It was why the implementation
wasn't available to the compiler at the time. It turns out I probably was
correct regarding the assumption that the implementation was part of the
translation unit. But it seems the forward declaration was not sufficient
to get the compiler to postpone the attempt to process the class
definition. I think the answer is probably in Clause 2 of PL-C++03 (The
Standard).


In the simplest terms my question is this:
what's the formal rule that tells me this won't compile?

struct A;

struct D {
A a;
};

struct A {};

int main(){}
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
1807
by: Steven T. Hatton | last post by:
I will assume many people reading this would never create anything similar to the example below. So let me preface this with _*IF*_ you were in a situation where you had to chose between using #includes or forward declaring each class in diamond.h, which would you choose? Why? If there is something fundamentally wrong with the way I've approached the structure of this example, I am interested to know. As for preferences and tastes, I...
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.