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

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 1743
The Cool Giraffe <gi******@viltersten.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******@viltersten.comwrote in news:568ccjF27ge49U1
@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******@viltersten.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.comwrote:
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******@viltersten.comwrote in message
news:56*************@mid.individual.net...
Marcus Kwok wrote/skrev/kaita/popisal/schreibt :
>The Cool Giraffe <gi******@viltersten.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
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...
9
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...
21
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...
9
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 { /* ... */ }; ...
21
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...
5
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...
8
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...
4
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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...
0
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...

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.