http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59117
Bug ID: 59117 Summary: Is the default constructor generated by the compiler actually called? Product: gcc Version: 4.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: objc++ Assignee: unassigned at gcc dot gnu.org Reporter: konevaanna2012 at gmail dot com Dear sirs! I am Anna Koneva from Moscow, Russia. I have faced with some problem using XCode 4.6.3 and the LLVM GCC 4.2. I use the following code. Let's say we have an Objective-C class and a C++ class: FileObjCClass.h: #include "CppClass.h" @interface ObjCClass :NSObject { CppClass cppClass; } - (void)SomeObjCMethod; @end File ObjCClass.mm: #import "ObjCClass.h" @implementation ObjCClass - (void)SomeObjCMethod { cppClass.someMethod(43); } @end FileCppClass.h: classCppClass { private: int _var; public: CppClass(); int someMethod(int param); }; File CppClass.cpp: #include "CppClass.h" #include <iostream> using namespace std; CppClass::CppClass() { _var = 26; } int CppClass::someMethod(int param) { cout << "param = " << param << endl << "_var = " << _var << endl; return param; } File main.mm: #import <Foundation/Foundation.h> #import "ObjCClass.h" int main(int argc, const char * argv[]) { ObjCClass *obj = [[ObjCClass alloc]init]; [obj SomeObjCMethod]; [obj release]; return 0; } If you switch to «LLVM GCC» from «Apple LLVM compiler» (selected by default after the creation of the project) a checkbox appears in the project settings (which is not available for Clang) «Call C++ Default Ctors / Dtors in Objective-C» (GCC_OBJC_CALL_CXX_CDTORS, -fobjc-call-cxx-cdtors). Case 1. The checkbox is off. Results: compiler warnings «Type 'CppClass' has a user-defined constructor» «C++ constructors and destructors will not be invoked for Objective-C fields» The sequence of calls (in order from top to bottom): — [ObjCClass init] — [ObjCClass SomeObjCMethod] CppClass::someMethod(int) What is interesting, the program succeeds and does not crash. In this case, our constructor (CppClass::CppClass) actually is not called, but the object is still created. It seems that the constructor generated by the compiler is called, but the ours is ignored. So my question - is it really called the constructor generated by GCC? How can I prove it? Can I see it in the assembly listing or somehow else? Or am I totally wrong? In this case the variable _var has a value 0 both in release and in debug modes, though in C++ member variables are not initialized by default. Case 2. If the checkbox is on, then our constructor will be successfully invoked. Results: no compiler warnings The sequence of calls: _objc_rootAllocWithZone object_cxxConstructFromClass(objc_object*, objc_class*) — [ObjCClass .cxx_construct] CppClass::CppClass() — [ObjCClass init] — [ObjCClass SomeObjCMethod] CppClass::someMethod(int)