domingo, 23 de março de 2014

Diferença entre #pragma once e #ifndef

Diretivas de Compilação #pragma once e #ifndef tem a mesma função: gerar menos código, evitar conflito e aumento de velocidade. Isso significa que o compilador ira incluir o arquivo apenas uma vez na compilação do arquivo de código fonte. A unica diferença é que o #ifndef fornece maior portavilidade para o código.

Exemplo de uso do #pragma once

-------------------------------------------------
File “grandfather.h”
#pragma once

struct foo {
int member;
};
-------------------------------------------------

-------------------------------------------------
File “father.h”
#include "grandfather.h"
-------------------------------------------------

-------------------------------------------------
File “child.c”
#include "grandfather.h"
#include "father.h"
-------------------------------------------------

Exemplo do uso do #ifndef

#ifndef TIME_H
#define TIME_H

//defina o header file "Nomearq.h"
//Código

#endif


Why is “using namespace std;” considered bad practice?

This is not related to performance at all. But consider this: You are using two libraries called Foo and Bar:
using namespace foo;
using namespace bar;
Everything works fine, you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.
If you have used foo::Blah() and bar::Quux() then the introduction of foo::Quux() would have been a non-event.