How do you properly use namespaces in C++?

Namespaces are packages essentially. They can be used like this: namespace MyNamespace { class MyClass { }; } Then in code: MyNamespace::MyClass* pClass = new MyNamespace::MyClass(); Or, if you want to always use a specific namespace, you can do this: using namespace MyNamespace; MyClass* pClass = new MyClass(); Edit: Following what bernhardrusch has said, I … Read more

How to properly overload the

Just telling you about one other possibility: I like using friend definitions for that: namespace Math { class Matrix { public: […] friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix) { […] } }; } The function will be automatically targeted into the surrounding namespace Math (even though its definition appears within the scope of … Read more

What are namespaces?

Namespacing does for functions and classes what scope does for variables. It allows you to use the same function or class name in different parts of the same program without causing a name collision. In simple terms, think of a namespace as a person’s surname. If there are two people named “John” you can use … Read more

Getting all types in a namespace via reflection

Following code prints names of classes in specified namespace defined in current assembly. As other guys pointed out, a namespace can be scattered between different modules, so you need to get a list of assemblies first. string nspace = “…”; var q = from t in Assembly.GetExecutingAssembly().GetTypes() where t.IsClass && t.Namespace == nspace select t; … Read more

Getting “type or namespace name could not be found” but everything seems ok?

This can be the result of a .Net framework version incompatibility between two projects. It can happen in two ways: a client profile project referencing a full framework project; or an older framework version targeting a newer framework version For example it will happen when an application is set to target the .Net 4 Client … Read more

Namespace + functions versus static methods on a class

By default, use namespaced functions. Classes are to build objects, not to replace namespaces. In Object Oriented code Scott Meyers wrote a whole Item for his Effective C++ book on this topic, “Prefer non-member non-friend functions to member functions”. I found an online reference to this principle in an article from Herb Sutter: http://www.gotw.ca/gotw/084.htm The … Read more

What are inline namespaces for?

Inline namespaces are a library versioning feature akin to symbol versioning, but implemented purely at the C++11 level (ie. cross-platform) instead of being a feature of a specific binary executable format (ie. platform-specific). It is a mechanism by which a library author can make a nested namespace look and act as if all its declarations … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)