-2

So I'm new to Qt and to C++ in general but I'm trying to figure out what's going on in the following code.

I've a header file and source file charinfowindow. In the source file I have:

using namespace std

and in the header file I have

namespace Ui {
    class CharInfoWindow;
}

I've tried to find out what these pieces of code are doing online but I'm just finding resources saying how to do it.

If somebody could tell me firstly what these pieces of code are doing and secondly why they're doing it. It would be greatly appreciated.

Thanks

Paddy O'Brien
  • 43
  • 1
  • 6
  • `using namespace std` is a bad idea in general, https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?rq=1 and the latter is a forward declaration (as ChrisMM says). That would be "normal" in a Qt-Designer based application where the UI is usually (compiled by designer into) in `namespace Ui` – Adriaan de Groot Mar 06 '21 at 14:47

2 Answers2

1

For what it's worth, using namespace std is considered a poor practice. If you don't want to say std::cout all the time, you can:

using std::cout; using std::endl;

etc. This means you don't pull the entire world into scope.

But in short:

using namespace std;

Just means you don't have to put std:: in front of everything like you do without it.


The other half -- the forward declaration -- that simply puts your class in the given namespace without making the entire include file one big:

namespace UI {
... everything
}
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
0

“using namespace std” means that in this source file, you don’t have to use std:: prefix when using types from this namespace (you can write string instead of std::string, for example.

“class xxx;” in the header file is a “forward declaration” of a class xxx. It tells the compiler “there exists a class xxx”, so that it can count on it. However, the definition of the class is elsewhere (in another header file).

Jiri Volejnik
  • 1,034
  • 6
  • 9