views:

265

answers:

3

Hi,

Every C++ books that I've read says something like this:-

  1. All C++ programs have a main() function.
  2. main() function is the starting point for all C++ programs.
  3. All C++ programs begin its execution from the main() function.

However, I found that Windows programs written using MFC do not have any main() function. It use WinMain() function as the program starting point.

So can I say that Windows program written using MFC is not a C++ program? Then, what kind of program it is?

Thanks.

+1  A: 

They still are C++ programs; the main function is defined within something that is linked to the executable.

icktoofay
In other words, it is hidden from you, the programmer, by visual studio. The main() is buried in the framework.
Muad'Dib
Well.. that's not entirely true. It _could_ be the way it worked, but it's not in actuality the way it works. You won't find a standard style `main` function anywhere on the call stack (or even a function named `main`). And it's really the CRT and the linker rather than Visual Studio, somewhat exacerbated by MFC.
Logan Capaldo
@Logan: Oh. I read somewhere that the `main` function was linked in, but you're probably right.
icktoofay
+1  A: 

There's no reason why the operating system (or runtime library) can't decide to call a different function when a program starts instead of main(). In this case, Microsoft did it because they wanted to pass different parameters that were more relevant to the Windows starting conventions.

There's nothing wrong with this, you're still writing in C++, it just means the program is not compliant with the ISO C++ standard. You were, of course, a bit outside the standard to begin with when you started using MFC at all.

(You've run into a real wierd pitfall of programming C++. Most other languages have no such concept as a program using only standard language features, but being in violation of the language's standard. C++ does, and there are a number of ways to get into that strange limbo.)

Ken Bloom
+1  A: 

The answer to this question is a little more complicated than yes or no. It depends largely, on how strict you make your definition of "C++ program". A Windows subsystem program built with MSVC will not generally have a main function. MFC is a C++ language framework for building Windows subsystem programs (at least in contemporary practice). For the purposes of "Is a program written without a main fuction using a C++ compiler a C++ program, and if not, what is it?" MFC is irrelevant.

The main function, can be talked about in terms of "freestanding" and "hosted" implementations. Only "hosted" implementations are required by the standard to have main as an entry point. That said, you'd be hard pressed to call Microsoft's implementation of the CRT and the language "freestanding" with a straight face.

So we could make the question more specific "Is an MFC application a conforming, hosted C++ program?" and the answer to that would be "Technically, very technically, no."

Re: freestanding vs. hosted:

Any run of the mill C++ user land application is generally going to be hosted, that is have the benefit of the standard library et. al. Examples of freestanding scenarios might in an embedded system or an operating system kernel. For example when writing an OS kernel you can't rely on the presence of functionality like malloc or new because you're implementing the services (virtual memory, processes, etc.) that will ultimately used to implement things like malloc and new.

Logan Capaldo
Hi Logan, can you elaborate more on "freestanding" and "hosted" implementations? Under what condition we will use "freestanding" implementation and under what condition we will use "hosted" implementation?
kwc