require_once

is a PhP languange

What's the equivalent in C++
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?
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

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.
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.
Visual C++ has

#pragma once

Not all implementation support that

Bravo microsoft!
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.
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 
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?
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
Topic archived. No new replies allowed.