I'm trying to understand namespaces a little better in C++. My first object oriented language is Objective-C so I compare other languages to it. It seems to me that C++ namespaces are similar to iOS frameworks like UIKit and Foundation. Which are files containing lists of other header files. I would also like to know if C++ namespaces are similar to C# and/or Java namespaces. I know of only one difference that C++ namespace has is the use of the (::) notation vs. the (.) notation in C# and Java. I also know that (unlike Objective-C) you can use the name of a namespace to call its class and function: like "System.Console.WriteLine()" or "std::cout". I appreciate any advice. Thanks
A C++ namespace is just that: something that separates names of one kind from names of another kind. The key point of a namespace is to avoid "name collisions" - two functions or variables having the same name, and thus the compiler/linker not knowing which one you mean.
A framework is a set of functions and/or objects that allow you to perform some partiuclar (set of) task(s). It could be:
UI framework that helps you do menus, windows and dialog boxes.
compiler framework that helps if you want to build a compiler.
a framework for doing linear algebra.
connect to a database-engine and formulate SQL queries.
webtoolkit for building applications that talk to websites or displays webpages.
and millions of other things.
A framework typically is implemented inside a namespace, to avoid it colliding with other parts of your code or some other framework.
The standard C++ library is implemented in the std namespace, it is not strictly speaking a framework, but rather a collection of basic functionality that most applications will need in some way.
This is a pretty simplified view, as a complete descriptions of the two concepts is probably a few days worth of effort...
Related
I am currently designing and writing a custom coding language in Python 2.7 and as I am implementing more and more functions, I keep realising that I have more functions to implement.
I am currently wondering two things.
The first is what is the smallest amount of functions to implement in a coding language for it to work and be expandable like other coding languages by having user created modules and functions. (Personally I would limit this, for Python functions, to if, else, import, def)
The second thing is what other functions should be implemented to make it easier for the user but aren't necessarily required.
The smallest number of functions needed to allowed programmers to expand a language is one. They need a way to connect to compiled object files *.o and shared libraries *.so. For example, if they can connect and import functions from libc, then all of the system functions will be available to them.
One functionality that makes things easier, especially for teams, is namespaces. If each module is a separate namespace, then there is no need to coordinate names among team members.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“using namespace” in c++ headers
I'm a c# developer in the WPF world, but I've decided to do all of my Winrt development in C++.
One thing im getting confused over is the use (or not) of using statements in header files (as opposed to typing out the fully qualified type names).
Its so much easier to have using statements in header files. A lot of the Microsoft Winrt samples include using statements in headers which keeps the code clear and easy to read.
However, I've been going through the c++ samples in the preview of John Petzolds new book, and he demands that headers be free of all using statements.
I dont understand the pro's and con's, and when I google it I get lots of opposing opinions.
Can anyone give me a definitive explanation of this issue as it related to the Winrt world (im not interested in c++ in other environments)
Thanks
In C++, it is very much customary to avoid using namespaces in header files, for several reasons.
Unlike C#, C++ doesn't have a nice tidy module system. Anything you do in a header affects every file which includes it. So if one header has a using namespace, if effectively pollutes not just the header file itself, but all files which include it. That can lead to surprising name clashes.
So in C++, the most common convention is this: never put using namespace in a header. In source files, you can do it if you want to, but many people avoid it even there.
Another reason is that this can actually aid readability:
If I refer to a vector<int>, it's not really clear what that is. It could be any vector class defined anywhere in any of my included headers.
But if I write std::vector<int>, then I know that this is the standard library header. It tells me where a type comes from. I think that makes the code both clearer and more easy to read.
Of course, there are tricks you can use to make this more manageable, if you refer to certain types a lot, or if certain namespaces have long, verbose names.
Long namespace names can be aliased:
namespace foo = VeryLongNamespaceName;
// now I can do foo::bar instead of VeryLongNamespaceName::bar
or individual names can be imported from a namespace:
using std::cout;
// now I can refer to `cout` without the std prefix
Of course, it is not a coincidence that C++ developers tend to use flat namespace hierarchies and short namespace names.
Nearly all of the C++ standard library is in the std namespace, which is a bit easier to type than the .NET equivalent. (std::vector<T> vs System.Collections.Generic.List<T>). Short names and a flat hierarchy means that you can actually live without using namespace statements.
Can anyone give me a definitive explanation of this issue as it related to the Winrt world (im not interested in c++ in other environments)
There is no such thing as "the WinRT world" in this context. If you write C++ code in your WinRT application, then you should follow C++ conventinos and best practices. If you write C# code in your WinRT application, then you should follow C# best practices, and so on.
Nowhere do WinRT guidelines state "please disregard everything you would normally do in the language you're using". WinRT is an API, not a language.
As for why Microsoft's samples do otherwise: they're samples. Their goal is to illustrate specific concepts and give you a starting point, and not to teach you to write good code in general. It is assumed that you can take the concepts they illustrate, and fit it into your own code without copying the various shortcuts taken in the samples. Many samples also omit error handling code for the same reason. It's irrelevant for the purposes of the sample, but certainly not irrelevant in a real application.
Hey so for one of my c++ projects I need to develop a 4-5 window application.Now the issue is that currently all of my programs tasks are divided into classes, and I have tested them by passing 'dummy' values and returning print results. That's all fine and working, however now as I want to introduce a GUI interface it presents me with the problem of how my processing should communicate with the front end, since winAPI is initially meant for c and not object oriented language.
What I am thinking of doing, and have a feeling is going to be a tedious task, to make a class which does the win api's registrations and methods for me. Is there any other alternative to this ???
I was looking at integrating Qt into eclipse but I think they stopped providing the library for eclipse, because I couldn't find a download for the library anywhere, not even on the Qt download page.
Well, if you want to use Win32, then you have to do all the stuff that Win32 needs you to do. It's a rather low level API, so you just have to take care of a lot of details.
However, don't over-engineer things. You don't have to write a generic C++ wrapper for Win32... you just have to make a GUI for your program.
If your problem is only that classes are not supported by C, then simply replace the keyword class with the keyword struct. Just make sure to declare the access types of all variables (private, public, protected), that way it becomes interchangeable (Works for both). The only difference between them is the default access type, which for classes is private. In case you used other syntax that is unique to C++, then this wouldn't work.
There are code generators from C++ to C as well. The optimal solution would be to create a GUI using an IDE specifically for that purpose which uses C++ as its base language. MFC works well, but it is not open-source and you will need a considerable knowledge on inheritance and comfort with "class typecasting". Using the included wizards in Visual Studio will help.
Try the first option; it will possibly work the same way.
I'm working on a fairly large project written primarily in C++ using MFC. We are tasked to gradually port this application to use Qt. Years ago, a wrapper around much of our functionality was written using COM. I feel using the COM wrapper from the new Qt code will help isolate code that would force dependencies on MFC. Unfortunately, we're also being asked to ween our use of COM/ActiveX. So, introducing new consumers of our COM wrapper in Qt isn't ideal. Visual Studio has a class wizard that will generate a C++ class based on an interface in a TLB file, but it's dependent on MFC and the interface still exposes COM (LPDISPATCH, SAFEARRAY*, etc).
With all that said, does anyone know of a tool (free or commercial) that will take a Microsoft IDL file and convert it to C++, who's interfaces aren't dependent on MFC nor COM?
and working with the code generated by midl.exe
That's hang-up number one. Midl.exe does not generate code, it only generates declarations. Pure virtual classes in C++, only method declarations with no implementations. Either to a .h file or to a .tlb type library file. The type library is handy because it is easy to read by tooling, having a restricted sub-set of COM called Automation. And implemented by just about any language runtime on Windows.
Key point is that these are just declarations, the glue that makes code written in different modules and/or different languages or class libraries work together. Very important in large projects, interfaces tie the pieces together.
Our system architect learned of our approach and advised that we find a way to not add these COM-specifics to our new Qt projects.
That's singularly unhelpful advice. "Don't do that" is something my doctor tells me when it hurts to put my arm behind my back. I can live with that, I have a good alternative and can just turn around. In your case I would have to demand more from the architect. He's messing with the body parts, he's separating the torso from the legs and head and feet and hands. Brain utterly disjointed. The very glue that makes the different chunks of code you have now work together. Break that interface and you'll seriously break your app, Netscape style.
Beware of the astronaut architect (another Spolsky favorite) that's happy to force you into something that he understands but doesn't have to implement. Demand a reasonable alternative, an architectural approach since breaking the interfaces has a deep architectural impact on your app. Those MFC classes that everybody implemented from the interfaces are pretty much junk when you change the interface. Rewriting them all into Q classes is going to seriously keep you unproductive for a while. And is devastatingly boring code to write. Only to produce the same thing, with more bugs. Things You Should Never Do, part 2.
If you are able to keep using Visual C++, a solution would be to use the plain compiler support for COM.
You need to #import your TLB file
http://msdn.microsoft.com/en-us/library/8etzzkb6%28v=vs.100%29.aspx
Then you can make use of the COM compiler support to handle the COM object instances,
http://msdn.microsoft.com/en-us/library/h31ekh7e.aspx
I am quite confused by looking at the boost library, and stl, and then looking at people's examples. It seems that capitalized type names are interspersed with all lowercase, separated by underscores.
What exactly is the way things should be done these days? I know the .NET world has their own set of conventions, but it appears to be completely different than the C++ sphere.
What a can of worms you've opened.
The C++ standard library uses underscore_notation for everything, because that's what the C standard library uses.
So if you want your code to look consistent across the board (and actually aren't using external libraries), that is the only way to go.
You'll see boost use the same notation because often their libraries get considered for future standards.
Beyond that, there are many conventions, usually using different notations to designate different types of symbols. It is common to use CamelCase for custom types, such as classes and typedefs and mixedCase for variables, specifically to differentiate those two, but that is certainly not a universal standard.
There's also Hungarian Notation, which further differentiates specific variable types, although just mentioning that phrase can incite hostility from some coders.
The best answer, as a good C++ programmer, is to adopt whatever convention is being used in the code you're immersed in.
There is no good answer. If you're integrating with an existing codebase, it makes sense to match their style. If you're creating a new codebase, you might want to establish simple guidelines.
Google has some.
It's going to be different depending on the library and organization.
For the developer utility library I'm building, for example, I'm including friendly wrapper modules for various conventions in the style of the convention. So, for example, the MFC wrapper module uses the 'm_typeMemberVariable' notation for members, whereas the STL wrapper module uses 'member_variable'. I'm trying to build it so that whatever front-end is used will have the style typical for that type of front-end.
The problem with having a universal style is that everyone would have to agree, and (for example) for every person who detests Hungarian notation, there's someone else who thinks not using Hungarian notation detracts from the basic value of comprehensibility of the code. So it's unlikely there will be a universal standard for C++ any time soon.
Find something you feel comfortable with and stick with it. Some form of style is better than no style at all and don't get too hung up on how other libraries do it.
FWIW I use the Google C++ style guide (with some tweaks).
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml