开发者

Windows program written using MFC is a C++ program?

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

  1. All C++ programs have a main() function.
  2. main() function is the star开发者_如何转开发ting 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.


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 function 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.


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


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜