"Allocating memory" and "assigning memory location" has nothing to do with anything here. You are asking about how objects that are members of another class get constructed. Trying to pull in the subject of memory allocation here only confuses things. Advanced C++ techniques allow objects to be repeatedly constructed and destroyed "in the same place" (via placement new and explicit destructor invocations). Actual memory locations, and object constructions/destructions, are completely immaterial. They have nothing to do with each other, and it will be helpful if you simply forget everything about memory locations, and focus only on the topic of object construction and destruction. Having said all of that: Show
An object constructor is responsible for constructing all members of the object. That's the rule. No exceptions. Full stop.
This will work, but this also does this in a confusing member. This spells out the following sequence of events: a
temporary
This shorter, direct syntax, describes exactly what's going on in a simple, concise manner: the object's constructor constructs all members of this object, namely just one, here. It's called
If a constructor does not have a member initialization section, or does not list the object's member in the initialization section, the corresponding class members' default constructors get called. That's it, that's all there is to it: your class *here the class is called Skip to main content This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
In this articleInitializing C++ Class Members and Incorporating the Places Bar in your MFC Apps Paul DiLascia Q I have a question about initializing class members in C++. I see a lot of code (your column included) that does this: and in other places I see it written this way:
Some of my programmer friends say that it's better to do it the second way, but no one knows why. Can you tell me the difference between these two methods of initializing class members? A Technically your friends are right, but in most cases it makes no difference. There are two reasons to
use the second syntax, which is called a member initialization list: one is necessity, the other efficiency. Since CMember has an explicit constructor, the compiler does not generate a default constructor (one with no arguments) and there is no way to create an instance of CMember without an integer. How do you initialize CMember if it's a member of another class? You must use the member initialization list. There's simply no other way to pass the argument to m_member. The same is true if the member is a const object or reference. According to the rules of C++, const objects and references cannot be assigned; they can only be initialized. or Is there any difference between these methods? Yes. The compiler always insures that all member objects are initialized before the body
of the constructor executes, so in the first example the compiled code will call CString::CString to initialize m_str before control reaches the assignment statement. In the second example, the compiler generates a call to CString:: CString(LPCTSTR), passing "yada yada" as the string. The upshot is that in the first example two CString functions are called (constructor and assignment operator), whereas in the second example only one function is called. or Note that the second snippet is decidedly non-object-oriented. You might think that the previous code would first assign m_y=i and then m_x=m_y so that both m_x and m_y would have the same value. But the compiler initializes m_x first then m_y, since that's the order in which they are declared. The result is that m_x will have an unpredictable value. My example is contrived to illustrate the point, but there are times when this bug arises more naturally. There are two ways to avoid it. First, always declare members in the order you want them initialized. Second, if you decide to use an initializer list, always list the members in the same order they're declared. This will help you avoid confusion. Q I've just installed Windows® 2000 Release Candidate 1 on a few machines and am wondering how to get the new Open dialog with the Outlook-style bar on the left side (see Figure 1) in my MFC applications. Figure 1 The New Open Dialog Can I just set a flag, or do I need new header files and a new common dialog DLL? I've noticed that some older apps like Notepad seem to get the new Open dialog without recompiling, but not MFC apps. Ideally, I want an app that uses the old dialog on Windows 9x and Windows NT®, and the new dialog on Windows 2000. Warren Stevens A I'm afraid there's no happy answer to this one. The new Open dialog in Windows 2000, which contains the "Places" bar on the side, is implemented in a new version of commdlg.dll. The function that displays it is GetOpenFileName, the same function that was used in Windows 9x and Windows NT. However, GetOpenFileName now uses a new version of OPENFILENAME, the struct that passes information between your app and the dialog. The new struct has some additional members: Yes, that's right. Windows 2000 bumps the Windows version number to 5, which in hexalese is 0x500. If you compile your program with _WIN32_WINNT = 0x0500, then OPENFILENAME gets the three new members. The first two are reserved. The third flags field, FlagsEx, has a new OFN_EX_NOPLACESBAR flag that suppresses the Places
bar. Windowsâ€"or more precisely, commdlg.dllâ€"uses the first member of OPENFILENAME, lStructSize, to determine which dialog to display. If lStructSize is 76 (the old size), Windows runs the old dialog; if it's 76+3�4=88 (the new size), it runs the new dialogâ€"or so the friendly Redmondtonians originally informed me. During my explorations, I discovered that this isn't the whole picture. The size of this structure is whatever
the size of OPENFILENAME was when the friendly Redmondtonians compiled MFC42.DLL; in other words, the old size. Moreover, the MFC code is frozen for all time in MFC42.DLL, or NAFXCW.LIB if you're doing a static link. You can't just set m_ofn.lStructSize to the new size since CFileDialog has some other data members following m_ofn, which would then get overwritten by the new OPENFILENAME members. Since all through this exercise I was using the older version 0x400 SDK files (because I wanted to
build an app that runs on Windows 9x as well as Windows 2000), ofn.lStructSize gets the old size. When I compiled and ran this code, I got the old dialog on Windows 98 and the new dialog on Windows 2000â€"just like Notepad! So Windows 2000 is, in fact, smart enough to use the new dialog for old appsâ€"but not old MFC apps. It makes no sense. What's different about an MFC app? Paul DiLascia is the author of Windows++: Writing Reusable Windows Code in C++ (Addison-Wesley, 1992) and a freelance consultant and writer-at-large. He can be reached at or https://www.dilascia.com. From the April 2000 issue of MSDN Magazine. How do you initialize a class member in C++?There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.
Which type of member function may only be called from a function that is a member of the same class?A private member function may only be called from a function that is a member of the same class.
Which of the following is used to initialize data members?Constructor is used to initialize the data members of a | KnowledgeBoat.
Which operator can be used by an object to access members of the class?Accessing the members of the class (data and functions) is done using the dot (.) operator, which is also called as the member access operator. If obj is the name of the object and there is a function “display ()” in the class, then the function can be accessed as “obj. display ()”.
|