Re: Getting Started w/ Standard C++ in Microsoft VC++ 6.0?

From: Daniel James (wastebasket_at_nospam.aaisp.org)
Date: 01/06/05


Date: Thu, 06 Jan 2005 11:25:39 GMT

In article news:<41DC0AB5.8923FF67@eaw.com>, Bill Hoy wrote:
> I have a few years of experience using MFC and Microsoft Visual C++
> 6.0. As my company gets further into the software business they now
> want to start moving away from MFC, and use pure Standard C++.

Standard C++ is good - especially for back-end program logic - but MFC
makes programming for the Windows API *much* easier than using the API
directly.

I would start by separating the program into "GUI" and "back end"
subsystems and rewrite the "back end" system in standard C++. I wouldn't
even think about translating the "GUI" component, it's just not worth the
bother.

If you want to rewrite the GUI -- and I do mean "rewrite" not "translate"
-- so that your application will be portable to non-Windows systems you
should look at cross-platform toolkits such as wxWidgets (free, open
source) or Qt (commercial, free for open source projects on open source
platforms but damned expensive on Windows). Once your "back end" subsystem
is in standard C++ you should be able to use it with such a GUI toolkit
without further change.

> I see that I will have to use: "using namespace std" ...

You don't have to write "using namespace std" anywhere. That's a notational
convenience which may save some typing and make some of your sourcecode
cleaner, but many autorities on C++ say you should prefer explicit
qualification to "using".

That is, you should write:

   std::cout << "Hello world" << std::endl;
   
rather than:

   using namespace std;
   cout << "Hello world" << endl;
   
because that makes it explicit *which* cout and *which* endl (and which
operator<<, too) you mean, and removes any possibility of ambiguity if you
include files that define other objects with the same names.

Better than "using namespace ..." is to explicitly declare namespace
objects to be used, like this:

   using std::cout;
   using std::endl;
   cout << "Hello world" << endl;
   
"using namespace ..." can sometimes be the neatest way to write code, but
even then you must be aware of the potential for ambiguity that this
pollution of the global namespace introduces, and you should try to contain
its effects as much as possible (for example, by placing the "using"
declaration inside a specific function where you want its effects, rather
than at file scope). Above all never, ever, place a "using namespace ..."
directive in a header file.

For more discussion of this point see the last couple of sections of Herb
Sutter's "More Exceptional C++" (Addison Welsey).

> But I am not sure what else I have to set up in the Microsoft Developer
> Studio. Perhaps I have to start a new project/workspace that is just a
> W32 Application? What do I need to link to in the Project Settings >
> Link tab? What else do I need to do to so that it recognizes the
> namespace "std" ?

The only thing you have to do to get the compiler to recognize the std
namespace is include a header that declares something in that namespace.
The namespace keyword is part of the C++ language. You don't need to have a
special kind of project or a special kind of sourcefile - I use the
standard library in MFC projects all the time. It Just Works.

If you want to create a project that doesn't use MFC at all you can just
create a new project for either a "Win32 application" or a "Win32 comsole
application" (in VC6) and the standard library will be available.

You should be aware, though, that the VC6 compiler is now quite old, and
that it was released before the C++ standard was finalized. There are
things in the standard language that VC6 doesn't support and things in the
standard library that VC6 can't support correctly. If these things start to
become a problem then you should consider using VC7.1 (VS2003) which is a
*much* more standard-compliant compiler.

Cheers,
 Daniel.
 



Relevant Pages

  • Re: GCC
    ... That spec says what the names of the standard header ... It's important if you want to the use the namespace feature. ... what use is a compiler that does ...
    (Debian-User)
  • Re: Do you put functions in the global namespace? Other than main()
    ... namespace std or namespaces within namespace std unless otherwise ... A program may add template specializations for any standard ... partial) of a standard library template results in undefined behavior ... Everything was in an identifiable compartment, ...
    (comp.lang.cpp)
  • Re: GCC
    ... The header names changed explicitly to ... The Standard says that the standard ... that did not put iostream into the std namespace. ... and compilers change along with them. ...
    (Debian-User)
  • Re: Why not add namespace feature into standard C?
    ... features, I can't understand why not add this feature into standard C. ... macro in a namespace safe way. ... And so long as you don't define a function or macro called swap, ...
    (comp.lang.c)
  • Compiler limitation/bug on VC6 (only interesting if you want to try it on VC7.1, it works on VC8b2)
    ... On VC6, template definitions in nested anonymous namespaces cannot ... be seen within the same translation unit, unless the outer namespace is ... brought into scope with a 'using' directive. ... I've not found any posts on Gougle ...
    (microsoft.public.vc.language)