Archived

This topic is now archived and is closed to further replies.

mikef

Using STL and MFC at the same time.

Recommended Posts

I was wondering if it is possible to use MFC and STL at the same time and if so are there any benefits of doing so. Also, would you guys recomend learning the win32 api before learning MFC? [edited by - mikef on August 14, 2002 2:18:24 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sure, there''s no problem with that. I use maps of CString key, CString value all the time. CString provides a lot of handy functions for strings that I don''t have to write, and the STL provides an excellent storage mechanism.

MFC offers its own collection classes (CObArray, CStringList, etc.) but they are not nearly as powerful, flexible, and useful as an STL container.

Share this post


Link to post
Share on other sites
Thats good, I learning how to program, I used Practical C++ by QUE as a start, I now have STL Tutorial and Reference Guide, Second Edition, an Sams Teach yourself Visual C++ .NET in 21 Days.

Share this post


Link to post
Share on other sites
quote:
Original post by mikef
I was wondering if it is possible to use MFC and STL at the same time

Yes.
quote:

and if so are there any benefits of doing so.

You get to use standard C++ for the core of your application and limit MFC to only where it is necessary. It''s up to you to decide if that is a benefit.
quote:

Also, would you guys recomend learning the win32 api before learning MFC?

I''d recommend learning the Win32 API *over* learning MFC.

Share this post


Link to post
Share on other sites
quote:
original quote by SabreMan:
You get to use standard C++ for the core of your application and limit MFC to only where it is necessary. It''s up to you to decide if that is a benefit.



If you want to use MFC, it becomes the core of your application. You can''t simply use parts of it. Also, whether you write your program with pure API calls or the MFC framework, you are using standard C++. STL is just one part of C++ (a template library, hence the name). C++ is a language. MFC is a library of C++ classes that encapsulate the Win32 API.

You should definetly learn the Win32 API before learning MFC. MFC has its uses, but in order to get a firm grasp on how Windows works, you should learn the API.

-Mike

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
You should definetly learn the Win32 API before learning MFC. MFC has its uses, but in order to get a firm grasp on how Windows works, you should learn the API.


I disagree with that. It''s like saying you need to learn C before learning C++. In either case, it just comes down to what you''re comfortable with. I prefer calling methods of a CWnd object to calling a global function and passing in a HWND, so I use MFC.

I highly recommend reading MFC Internals by Scot Wingo & George Shepherd (I think...I''m away from my copy right now). This book is amazing! It really clears up a lot of what goes on behind the scenes in MFC, and greatly improves your ability to use it.

Share this post


Link to post
Share on other sites
quote:
Original post by doctorsixstring
If you want to use MFC, it becomes the core of your application.

Only if you make it so. In an architecture resembling model-view-controller (for example), it''s common to perceive the model as the functional core, with MFC being a potential candidate technology to implement the view. The idea of such an architecture is that either the model or view can be changed for another with only the controller layer having to change to handle the interactions. Allowing MFC classes into the controller layer obviously compromises the ability to cleanly switch to a different platform library.
quote:

You can''t simply use parts of it.

Of course you can. Are you saying you must use either all of it or none of it?
quote:

Also, whether you write your program with pure API calls or the MFC framework, you are using standard C++.

MFC and Win32 are both APIs which are not defined by the C++ Standard. MS provide C++ extensions with their compiler, and their libraries will not compile with those extensions turned off. In fact, their C++ Standard libraries depend on extensions, but at least those extensions don''t have to appear in user code. If you are writing an MFC application the way MS would have it, you are writing MS C++, *not* Standard C++. The point I''m making is that it depends on the project whether that is an issue.
quote:

STL is just one part of C++ (a template library, hence the name). C++ is a language.

And a library. The C++ Standard defines both.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Of course you can. Are you saying you must use either all of it or none of it?




If you would kindly make a .dsw or .sln of a project that just uses "a little bit" of MFC, zip it up, and send it to bross_46@hotmail.com, I''d really appreciate it.

I''ve been trying to use just a little bit of MFC without using the whole god damn thing for years now.

Share this post


Link to post
Share on other sites
I can''t see the point to use CString with STL, since std::string is far more powerful than CString.
If you have to use both at the same time, go for std::string.

(MFC is useful to make dialog windows)


Share this post


Link to post
Share on other sites
quote:
Original post by daerid
If you would kindly make a .dsw or .sln of a project that just uses "a little bit" of MFC, zip it up, and send it to bross_46@hotmail.com, I'd really appreciate it.

I've been trying to use just a little bit of MFC without using the whole god damn thing for years now.


I'm not sure what you're asking for. I have projects where I use bits of MFC all the time. What do you mean by "the whole damn thing"? That would imply that you are somehow forced to use things like CDBVariant in every application...

An example:

MFC:
CWnd Wnd;
CDC *pDC = Wnd.GetDC();

nonMFC:
HWND hWnd;
HDC hDC = GetDC(hWnd);

Mixed:
CWnd Wnd;
HDC hDC = GetDC(Wnd.GetSafeHwnd());

There are better examples, but that's the general idea.

[edited by - CrazedGenius on August 15, 2002 1:29:57 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
I''ve been trying to use just a little bit of MFC without using the whole god damn thing for years now.

Why? Do you have some strange compulsion that makes you use every feature of MFC?

Share this post


Link to post
Share on other sites
No.

Idunno, its just that every time I try to use things like CString in my project, it seems like its way too much of a hassle unless I create an MFC application from scratch.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Why? Do you have some strange compulsion that makes you use every feature of MFC?


Where did you get the idea that we mean "you must use every feature of MFC"? What we mean, is that you cannot simply create a pure Win32 application and add just a few MFC classes when you need them. If you want to use any MFC class in a program, you have to create an MFC project, which in turn encapsulates ALL of Win32 (WinMain, WndProc, etc.). Thus, If you want to use MFC, it becomes the core of your application. You can't simply use parts of it.

quote:
Original post by CrazedGenius
I have projects where I use bits of MFC all the time.


How? Do you mean that you actually created a non-MFC app and were able to add MFC classes (like CString) into it?

Lastly, when I describe MFC as the "core" of an application, I mean "core" as in the portion of the program that performs message handling and controls the overall execution loop. I am not referring to the functions which perform the majority of the app's logic.


-Mike

[edited by - doctorsixstring on August 15, 2002 2:11:32 PM]

Share this post


Link to post
Share on other sites
You could choose a Win32 Application from the "new project" menu, and then view the Project settings (alt+F7), where you should be able to find an option which includes MFC as ''a Shared DLL''.

Johan Ersvik

Share this post


Link to post
Share on other sites
MSVC 6.0 had a problem where you couldn''t use CString without including the rest of MFC--it actually just needs afxbeginthread and afxendthread. For MSVC 7.0 (.NET) they''ve made CString a templated class (I presume for unicode fun) and might have fixed this problem, though I haven''t tried it yet.

There are a few classes that are independent of MFC that you can use without making your project MFC, but generally, it''s true that you need the core window map (thread-local CWnd registry) to use the handy GDI classes, and the whole message-dispatching structure I believe is necessary for any of the window classes.

OTOH, this has nothing to do with STL. There''s a small amount of overlap between MFC and STL--that being CString and some of MFC''s container classes. There''s generally no reason to use MFC''s classes--STL''s are superior in every way except that std::string doesn''t have a sprintf-type (e.g. CString::Format) function (which some people would say makes it better).

Share this post


Link to post
Share on other sites