Hi, I am a newbie of objective-c. Hopes someone can give me some question about the virtual function question.
I found in objective-c all fuction is virtual functin (in C++ term). And this turns out that it is impossbile to implement a special Bridge Design Pattern. (efficiently get private object from public pointer, and public pointer is not a real object). Is there any way to make an member function (or method) become non-virtual function just like default in C++ ?
Here is an example of C++ code that works.
class Foo {
public:
void foo(){
printf("Foo me %X\n", this);
}
};
int main(){
Foo* foo = (Foo*)malloc(1);
foo->foo();
}
-------------- but this code is not working in objective-c -------------
#include <objc/Object.h>
@interface Foo : Object
{
}
+ new;
- foo;
@end
@implementation Foo
+ new
{
self = [super new];
return self;
}
- foo
{
printf("Foo me %X\n", self);
}
@end
int main(){
{
Foo* foo = [Foo new];
[foo foo];
//this can work
}
Foo* foo = (Foo*)malloc(1);
[foo foo];
}
I found in objective-c all fuction is virtual functin (in C++ term). And this turns out that it is impossbile to implement a special Bridge Design Pattern. (efficiently get private object from public pointer, and public pointer is not a real object). Is there any way to make an member function (or method) become non-virtual function just like default in C++ ?
Here is an example of C++ code that works.
class Foo {
public:
void foo(){
printf("Foo me %X\n", this);
}
};
int main(){
Foo* foo = (Foo*)malloc(1);
foo->foo();
}
-------------- but this code is not working in objective-c -------------
#include <objc/Object.h>
@interface Foo : Object
{
}
+ new;
- foo;
@end
@implementation Foo
+ new
{
self = [super new];
return self;
}
- foo
{
printf("Foo me %X\n", self);
}
@end
int main(){
{
Foo* foo = [Foo new];
[foo foo];
//this can work
}
Foo* foo = (Foo*)malloc(1);
[foo foo];
}