About Monkey 2 › Forums › Monkey 2 Programming Help › how to get an extern class that extends bbObject and not void?
This topic contains 4 replies, has 3 voices, and was last updated by 
 codifies
 2 years, 5 months ago.
- 
		AuthorPosts
 - 
		
			
				
September 18, 2016 at 11:21 am #4002
I could not compile some code with an external class that doesn’t extends void but bbObject: “(source type is not polymorphic)” error message
In the manual it looks like it’s possible:Extern classes are assumed by default to be real monkey2 classes – that is, they must extend the native bbObject class.
However, you can override this by declaring an extern class that extends Void. Objects of such a class are said to be native objects and differ from normal monkey object in several ways:
A native object is not memory managed in any way. It is up to you to ‘delete’ or otherwise destroy it.
A native object has no runtime type information, so it cannot be downcast using the Cast<> operator.
[/crayon]Monkey12345678910111213141516171819[crayon-5cba8d8a98aaf649733881 inline="true" ]// external.h#ifndef EXTERNAL_H#define EXTERNAL_Hclass Foo{private:int prv;public:Foo(){prv=0;}void Set(int i) {prv=i;}int Get() { return prv; }int MyM(int i) {return prv*i;}};#endif[/crayon]Monkey1234567891011121314151617181920[crayon-5cba8d8a98ab4769638422 inline="true" ]' external_non_void.monkey2#Import "<std>"#Import "external.h"ExternClass FooMethod Set(i:int)Method Get:Int()Method MyM:int(i:int)EndPublicFunction Main()Local Fofo:FooFofo=New Foo()Fofo.Set(10)Print Fofo.Get()Print Fofo.MyM(7)EndTried with “extends Object” and “extends monkey.types.Object” too
With “extends void” the code will work.error message:
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849[crayon-5cba8d8a98ab8205343255 inline="true" ]mx2cc version 1.0.6***** Building app 'C:/Users/poilux/Desktop/extv/external_non_void.monkey2' *****Parsing...Semanting...Translating...Compiling...Build error: System command 'g++ -std=c++11 -m32 -O3 -DNDEBUG -I"C:/Users/poilux/Desktop/monkey2-106/modules/monkey/native" -I"C:/Users/poilux/Desktop/monkey2-106/modules/" -I"C:/Users/poilux/Desktop/extv/" -c -o "C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/build/_1src_2external_00non_00void_0external_00non_00void.cpp.o" "C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/src/external_0non_0void_external_0non_0void.cpp"' failed.g++ -std=c++11 -m32 -O3 -DNDEBUG -I"C:/Users/poilux/Desktop/monkey2-106/modules/monkey/native" -I"C:/Users/poilux/Desktop/monkey2-106/modules/" -I"C:/Users/poilux/Desktop/extv/" -c -o "C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/build/_1src_2external_00non_00void_0external_00non_00void.cpp.o" "C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/src/external_0non_0void_external_0non_0void.cpp"In file included from C:/Users/poilux/Desktop/monkey2-106/modules/monkey/native/bbmonkey.h:10:0,from C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/include/external_0non_0void_external_0non_0void.h:5,from C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/src/external_0non_0void_external_0non_0void.cpp:2:C:/Users/poilux/Desktop/monkey2-106/modules/monkey/native/bbgc.h: In instantiation of 'void bbGCMarkPtr(T*) [with T = Foo]':C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/src/external_0non_0void_external_0non_0void.cpp:16:25: required from hereC:/Users/poilux/Desktop/monkey2-106/modules/monkey/native/bbgc.h:302:15: error: cannot dynamic_cast 'p' (of type 'class Foo*') to type 'struct bbGCNode*' (source type is not polymorphic)bbGC::enqueue( dynamic_cast<bbGCNode*>( p ) );^In file included from C:/Users/poilux/Desktop/monkey2-106/modules/monkey/native/bbarray.h:7:0,from C:/Users/poilux/Desktop/monkey2-106/modules/monkey/native/bbmonkey.h:11,from C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/include/external_0non_0void_external_0non_0void.h:5,from C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/src/external_0non_0void_external_0non_0void.cpp:2:C:/Users/poilux/Desktop/monkey2-106/modules/monkey/native/bbobject.h: In instantiation of 'T* bbGCNew(A ...) [with T = Foo; A = {}]':C:/Users/poilux/Desktop/extv/external_non_void.buildv1.0.6/windows_release/src/external_0non_0void_external_0non_0void.cpp:19:34: required from hereC:/Users/poilux/Desktop/monkey2-106/modules/monkey/native/bbobject.h:49:15: error: cannot convert 'Foo*' to 'bbGCNode*' for argument '1' to 'void bbGC::endCtor(bbGCNode*)'bbGC::endCtor( p );^***** Fatal mx2cc error *****Internal mx2cc build errorAttachments:
September 18, 2016 at 11:59 pm #4012The c++ class must actually extend bbObject, eg:
Monkey12345678910111213141516171819#ifndef EXTERNAL_H#define EXTERNAL_H#include <bbmonkey.h>class Foo : public bbObject{private:int prv;public:Foo(){prv=0;}//virtual ~Foo() {} // Ensures to invoke actual object destructorvoid Set(int i) {prv=i;}int Get() { return prv; }int MyM(int i) {return prv*i;}};November 18, 2016 at 1:49 pm #5200the destructor is commented out?
if there needs to be code in the destructor do you have to
what should I do if I need to have code in the “Foo” destructor ?
November 18, 2016 at 8:37 pm #5222In this case, there is no need for a dtor as it’s a NOP and bbObject already declares a virtual dtor.
But you can add dtor code if you want. There is no need/way to call super.dtor as c++ already does this for you automatically after your dtor executes.
November 18, 2016 at 9:52 pm #5223yeah, gotcha, cheers Mark
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.