require_once

Jan 5, 2011 at 11:50am
is a PhP languange

What's the equivalent in C++
Jan 5, 2011 at 12:35pm
closed account (4Gb4jE8b)
well... considering i don't know PhP i have no idea from your question. Maybe if you told us what it does?
Jan 5, 2011 at 2:47pm
Looks like an inclusion guard.

In C++ add to the top of your headers:
#ifndef HEADERNAME_H
#define HEADERNAME_H


and on the last line of the header:
#endif

Jan 6, 2011 at 7:03am
Hmm... I am trying to find a way around it actually. No way isn't it :)

That seems to be pretty messy. Why can't there be a macro #includeonce is beyond me.
Jan 6, 2011 at 7:11am
That seems to be pretty messy. Why can't there be a macro #includeonce is beyond me.


The answer is simple. The PHP creator decide on a new keyword instead of re-use C syntax for similar features.

In Java it is the keyword import which is similar in concept.
Jan 10, 2011 at 12:59pm
Visual C++ has

#pragma once

Not all implementation support that

Bravo microsoft!
Jan 10, 2011 at 1:44pm
teguh123>#pragma once


Nevertheless, with #pragma once the one to be included contains the include rule, while in php with require_once the one, that includes, contains the rule.

#pragma once make no difference with Gladdok proposal.
Jan 10, 2011 at 2:53pm
Jan 10, 2011 at 11:41pm
I always use both. This gives the superpowers that #pragma once gives, but falls back on something better if it fails.

1
2
3
4
5
6
7
#pragma once
#ifndef FOOEY_HPP
#define FOOEY_HPP

...

#endif 
Jan 11, 2011 at 4:57pm
Actually I have an idea.

Why not put the guard on the file that includes like lionishy said

1
2
3
4
#ifndef FOOEY_HPP
#define FOOEY_HPP
#include "fooey.h"
#endif 


Tada....

No need a include guard on the .h file

with one small catch,

for some reason putting include guard on the .h file is far more popular.

Why?
Jan 11, 2011 at 6:03pm
Then in some other file someone in your team does the same again in a different file:
1
2
3
4
#ifndef F00EY_HPP
#define F00EY_HPP
#include "fooey.h"
#endif  

and it breaks your build. 2 days later, you find it has 0 instead of O.
Last edited on Jan 11, 2011 at 6:05pm
Topic archived. No new replies allowed.