473,692 Members | 2,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class redefintion problem

I have the following:

//A.h
class A {};

//B.h
#include "A.h"
class B : A {};
//C.h
#include "A.h"
class C : A {};

In my "main.cpp" I go:

#include "B.h"
#include "C.h"

and bad things happen. How do I aviod it?

I have heard something about IFNDEF but I don´t really
feel competent to use it. Trial and error would be very
time consuming.

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Mar 19 '07 #1
7 1758
The Cool Giraffe <gi******@vilte rsten.comwrote:
I have the following:

//A.h
class A {};

//B.h
#include "A.h"
class B : A {};
//C.h
#include "A.h"
class C : A {};

In my "main.cpp" I go:

#include "B.h"
#include "C.h"

and bad things happen. How do I aviod it?

I have heard something about IFNDEF but I don??t really
feel competent to use it. Trial and error would be very
time consuming.
The way ifndef works is, "if the following symbol is not defined, then
do whatever". So,

//A.h
#ifndef A_H
#define A_H

class A {};

#endif
//B.h
#ifndef B_H
#define B_H

#include "A.h"
class B : A {};

#endif
and similarly for C.h. Therefore, the first time it tries to #include
A.h, the A_H macro is not defined yet, so the first thing it does is to
#define A_H. When A.h is included again in the same translation unit
(your main.cpp), A_H is already defined, so it skips down to the #endif,
jumping over the definition of class A.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 19 '07 #2
"The Cool Giraffe" <gi******@vilte rsten.comwrote in news:568ccjF27g e49U1
@mid.individual .net:
I have the following:

//A.h
class A {};

//B.h
#include "A.h"
class B : A {};
//C.h
#include "A.h"
class C : A {};

In my "main.cpp" I go:

#include "B.h"
#include "C.h"

and bad things happen. How do I aviod it?

I have heard something about IFNDEF but I don´t really
feel competent to use it. Trial and error would be very
time consuming.
Use them:

// A.h

#ifndef INCLUDED_A_H
#define INCLUDED_A_H

class A {};

#endif

Repeat similarly for B.h and C.h (using INCLUDED_B_H and INCLUDED_C_H,
respectively).

Mar 19 '07 #3
The Cool Giraffe wrote:
I have the following:

//A.h
class A {};

//B.h
#include "A.h"
class B : A {};
//C.h
#include "A.h"
class C : A {};

In my "main.cpp" I go:

#include "B.h"
#include "C.h"

and bad things happen. How do I aviod it?
Three words: "double inclusion guards".
I have heard something about IFNDEF but I don´t really
feel competent to use it. Trial and error would be very
time consuming.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 19 '07 #4
Marcus Kwok wrote/skrev/kaita/popisal/schreibt :
The Cool Giraffe <gi******@vilte rsten.comwrote:
>I have the following:

//A.h
class A {};

//B.h
#include "A.h"
class B : A {};
//C.h
#include "A.h"
class C : A {};

In my "main.cpp" I go:

#include "B.h"
#include "C.h"

and bad things happen. How do I aviod it?

I have heard something about IFNDEF but I don??t really
feel competent to use it. Trial and error would be very
time consuming.

The way ifndef works is, "if the following symbol is not defined, then
do whatever". So,

//A.h
#ifndef A_H
#define A_H

class A {};

#endif
//B.h
#ifndef B_H
#define B_H

#include "A.h"
class B : A {};

#endif

and similarly for C.h. Therefore, the first time it tries to #include
A.h, the A_H macro is not defined yet, so the first thing it does is
to #define A_H. When A.h is included again in the same translation
unit (your main.cpp), A_H is already defined, so it skips down to the
#endif, jumping over the definition of class A.

My, my, my... I get the point and i'd like to thank you for the
answer. However, i can't stop wondering why such utility isn't
by default a part of the language. What can such monsterity
(in my, beginners eyes) be useful for? I'm sure there's a
point to that and it'd be easier to live if i knew.

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Mar 19 '07 #5
The Cool Giraffe wrote:
...
My, my, my... I get the point and i'd like to thank you for the
answer. However, i can't stop wondering why such utility isn't
by default a part of the language. What can such monsterity
(in my, beginners eyes) be useful for? I'm sure there's a
point to that and it'd be easier to live if i knew.
...
How exactly do you think it could look if it were "a part of the language"? In
this case we use include guards built from preprocessor directives and
preprocessor is a part of the language already. How much more "part of the
language" do you imply it could've been made?

Note that fully automatic unconditional protection from repetitive inclusion of
the same header file into the same translation unit (if that's what you meant)
is not an acceptable solution, since in some cases we might actually want to
perform such a repetitive inclusion.

One can also mention that it was once a popular alternative approach to handling
include dependencies, where interface-level headers were not supposed to be
included into other headers at all, meaning that in your example instead of
including 'A.h' into 'B.h' and 'C.h' you'd have to remember to include it into
'main.cpp'

#include "A.h"
#include "B.h"
#include "C.h"

Theoretically, this approach might lead to a better translation performance. It
doesn't appear to be a significant issue today, but it definitely was a much
bigger deal, say, a decade ago. Maybe that's a part of the answer to your "why"
question: at that time it was assumed that it is better to flag the
multiple-inclusion problem than to quietly handle it automatically or to support
(and thus encourage) it in any way by some "part of the language" feature.

--
Best regards,
Andrey Tarasevich
Mar 19 '07 #6
On 3/19/07 3:53 PM, in article 12************* @news.supernews .com, "Andrey
Tarasevich" <an************ **@hotmail.comw rote:
The Cool Giraffe wrote:
>...
My, my, my... I get the point and i'd like to thank you for the
answer. However, i can't stop wondering why such utility isn't
by default a part of the language. What can such monsterity
(in my, beginners eyes) be useful for? I'm sure there's a
point to that and it'd be easier to live if i knew.
...

How exactly do you think it could look if it were "a part of the language"? In
this case we use include guards built from preprocessor directives and
preprocessor is a part of the language already. How much more "part of the
language" do you imply it could've been made?
Quite a bit - at least enough to eliminate the current kludge (the one
cobbled together from a preprocessor macro and an #ifdef) that a C++
programmers often must resort to - given the lack of a more reasonable
facility in C++ to prevent a translation unit from including the same header
file more than once.

A built-in header guard would have to be concise, accurate and clear (in
other words, everything that the current macro include guard is not). A
simple preprocessor directive would probably do the trick:

#once

In fact, several C++ compilers (Metrowerks, Visual C++, gcc) offer a
"#pragma once" which has the same semantics envisioned for the #once
directive above. So adopting #once would formalize and standardize an
existing, proven solution. The #once directive would for that reason carry
little technical risk - yet make C++ sources more portable, and their
dependencies, easier to manage.
Note that fully automatic unconditional protection from repetitive inclusion
of
the same header file into the same translation unit (if that's what you meant)
is not an acceptable solution, since in some cases we might actually want to
perform such a repetitive inclusion.
The idea would be to add #once to those header files that should be included
once - and not add #once to any header file that should be included more
than once by a single translation unit. I imagine that most C++ programmers
will be able to remember this straightforward guideline.
One can also mention that it was once a popular alternative approach to
handling
include dependencies, where interface-level headers were not supposed to be
included into other headers at all, meaning that in your example instead of
including 'A.h' into 'B.h' and 'C.h' you'd have to remember to include it into
'main.cpp'

#include "A.h"
#include "B.h"
#include "C.h"

Theoretically, this approach might lead to a better translation performance.
It
doesn't appear to be a significant issue today, but it definitely was a much
bigger deal, say, a decade ago.
Since C++ templates have moved code into header files, the old maxim that
headers should not include other header files - is no longer a practical
goal.
Maybe that's a part of the answer to your
"why"
question: at that time it was assumed that it is better to flag the
multiple-inclusion problem than to quietly handle it automatically or to
support
(and thus encourage) it in any way by some "part of the language" feature.
If the compiler is able to ensure that a header file is included only once
for a translation unit, what is the benefit of forcing the programmer to
accomplish the same result - but have to do so by laboriously tinkering with
complex include dependencies in a trial-and-error search for an #include
order that works?

After all, compilers should make programmers' jobs easier, not the other way
around (now that a programmer's time typically costs much more than the
computer's). And header guards are a good example where C++ could do more
for the programmer. I would estimate that about 99% of C++ headers that I
have seen contain header guards of one sort or another. And to have so
widespread a need be met by embarrassing preprocessor hacks or compiler
specific pragmas - is no excuse for C++'s continued benign neglect of this
issue.

Greg

Mar 20 '07 #7
"The Cool Giraffe" <gi******@vilte rsten.comwrote in message
news:56******** *****@mid.indiv idual.net...
Marcus Kwok wrote/skrev/kaita/popisal/schreibt :
>The Cool Giraffe <gi******@vilte rsten.comwrote:
>>I have the following:

//A.h
class A {};

//B.h
#include "A.h"
class B : A {};
//C.h
#include "A.h"
class C : A {};

In my "main.cpp" I go:

#include "B.h"
#include "C.h"

and bad things happen. How do I aviod it?

I have heard something about IFNDEF but I don??t really
feel competent to use it. Trial and error would be very
time consuming.

The way ifndef works is, "if the following symbol is not defined, then
do whatever". So,

//A.h
#ifndef A_H
#define A_H

class A {};

#endif
//B.h
#ifndef B_H
#define B_H

#include "A.h"
class B : A {};

#endif

and similarly for C.h. Therefore, the first time it tries to #include
A.h, the A_H macro is not defined yet, so the first thing it does is
to #define A_H. When A.h is included again in the same translation
unit (your main.cpp), A_H is already defined, so it skips down to the
#endif, jumping over the definition of class A.


My, my, my... I get the point and i'd like to thank you for the
answer. However, i can't stop wondering why such utility isn't
by default a part of the language. What can such monsterity
(in my, beginners eyes) be useful for? I'm sure there's a
point to that and it'd be easier to live if i knew.
They are called "include guards". Microsoft in Visual C++ has a pragma that
does the same thing
#pragma once

Personally, I use include guards even though I'm using code that is specific
for a windows machine (DirectX). Once you're used to them, you instantly
recognize them in a header file and they become second nature. They are
really not any more hideous than any other programming.
Mar 20 '07 #8

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

Similar topics

1
2657
by: Steve | last post by:
Hello, I'm encountering an unexpected behavior when using the "new" modifier in a derived class to hide an inherited base class property. I use "new" intentionally so I can change the Type of the property in the derived class, and I can use the derived class as expected through standard instantiation. The unexpected behavior occurs when I try to set gather the PropertyInfo for the derived class property via Reflection. I get an...
9
4992
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
21
4068
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class A { std::vector<B*> Bs; public:
9
3674
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; template<class T> classSubject {
21
1976
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new ("Pear"); Basket.Items.Add(item); //------
5
3420
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
8
3477
by: tshad | last post by:
I cannot seem to get the asp:textbox to use classes. Style works fine. I am trying to set the textbox to act like a label in some instance so it doesn't have a border, readonly and the background is grey. I have a class set as: ..table2Label{ border-style:none; background-color:#F6F6F6; }
4
1684
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project that build a class library dll. Here we have a class called C We have one dependency and that is from the user control to the class library because in the constructor for class B in the user control we have a call to
3
3755
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
0
8547
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8970
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
8812
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
8810
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
6462
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
5822
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
4564
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2984
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
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.