#Include duplicate

Post Reply
Posts: 69
Joined: Sat Mar 28, 2015 4:45 am
Location: Quebec - Canada

#Include duplicate

Post by FredCailloux » Fri Feb 12, 2016 4:38 am

We often see preprocessor directive

Code: Select all

#if !defined BlaBla
	  define  BlaBla
But what if I must include in my Class definition a #Include directive and this inclusion is already defined inside another header of cpp code that include the very file that I am including in my definition. Is there not directive like #if !Included ? Just to garanty that the same file is not going to be included twice ?
Something like:

Code: Select all

#if !included BlaBla
	  include  BlaBla
Is there a way to do that or an equivalent ?
Also, What would be the consequence of including twice the same file ?
Prashant Tripathi :idea: Knowing is to the Self what knowledge is to the ego

User avatar
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK

Re: #Include duplicate

Post by majenko » Fri Feb 12, 2016 10:04 am

No, there is nothing like that. However, that is the role of the bits like this in headers:

Code: Select all

#ifndef _BLAH_H
#define _BLAH_H
... header contents here
That means that if a header is included twice the content is only actually there once since the second time it is included _BLAH_H has been defined so the #ifndef will fail.

When you do an include the preprocessor literally replaces that include directive with the contents of the included file, so no matter how deep your inclusions go you still end up with them inline in the same file.

Sent from my SM-T555 using Tapatalk
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Post Reply