Bilingual shenanigans
Bilingual shenanigans
- Subject: Bilingual shenanigans
- From: Oscar Morales Vivó <email@hidden>
- Date: Sat, 14 Sep 2002 00:37:31 +0200
However, it will write the space left for it with zeros, which may be
in many cases good enough.
Summarizing, you can use instances of C++ classes in Objective-C
classes as long as they respect the following:
- Having their data as all zeros leaves them in a proper state (or at
least a state that can be changed to a correct one in the init method).
- You don't have a non-trivial destructor for them, or at least you can
leave your object in the dealloc method in a state where it won't be a
problem that the destructor doesn't get called.
I'm not sure if having the object's allocated space zeroed means you
can't use instances of neither classes with virtual functions nor
classes with virtual base classes, but I'd bet that's the case
(particularly for the second case).
So usually using pointers and creating at init with new and destroying
at dealloc with delete is the safest bet. You can probably also use
auto_ptr and assign them NULL at the dealloc (not much of an
improvement). My code uses smart pointers as instance variables and
they work properly (they get zeroed, then they get assigned objects at
init, and they get assigned NULL at dealloc so the objects are freed).
I also use a number of structs without virtual functions that work
perfectly well.
On Friday, Sep 13, 2002, at 23:34 Europe/Madrid, email@hidden wrote:
I just tried it with 10.2/August Dev Tools.
The Objective-C++ compiler does allow a C++ object as an instance
variable of an Objective C object, but warns that any user-defined
constructor won't be called:
Compiling main.mm (3 warnings)
In file included from main.mm:10:
Parent.h:21: warning: type `Member' has a user-defined constructor
Parent.h:21: warning: type `Member' has a user-defined destructor
Parent.h:21: warning: C++ constructors and destructors will not be
invoked for Objective-C fields
#import <Foundation/Foundation.h>
#include <iostream>
class Member{
public:
Member() { std::cout << "Member() called." << std::endl; }
~Member() { std::cout << "~Member() called." << std::endl; }
};
@interface Parent : NSObject {
Member mMember;
}
- (id)init;
- (void)dealloc;
@end
--Squeegee
On Friday, September 13, 2002, at 05:01 PM, Finlay Dobbie wrote:
On Friday, September 13, 2002, at 09:00 pm, Ondra Cada wrote:
I don't know... Does Objective-C++ allow Objective-C classes to
have instance
variables that are C++ instances, as opposed to pointers to C++
instances?
I think so (but their constructors won't get called automatically).
I'm pretty sure it doesn't, actually.
-- Finlay
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.