manuel.gysin Posted October 13, 2011 Share Posted October 13, 2011 Hello Tried today the following: // Create a new vehicle test Vehicle test = new Vehicle(); // ferrari is no a Vehicle Car ferrari = test; // this does not work ferrari = Car(test); When I try to cast from Vehicle to Car the interpreter throws this: Parser::expectSymbol(): end of string expecting '.' Can somebody help me? Is it not possible to cast from mother class to child class? Thanks and greets Manuel Link to comment
Guest anet Posted October 14, 2011 Share Posted October 14, 2011 If Vehicle is a parent and Car is a child then yes, you can't cast Vehicle instance to Car instance. But you probably meant the following use case: class Vehicle { }; class Ferrari : Vehicle { void myUpdate() { } }; Vehicle test = new Ferrari(); Ferrari ferrari = Ferrari(test); Unigine script interpreter supports this type of casting, but it expects the following syntax: Ferrari(test).myUpdate(); Link to comment
manguste Posted October 14, 2011 Share Posted October 14, 2011 Please take a look at info on class casting. Link to comment
manuel.gysin Posted October 14, 2011 Author Share Posted October 14, 2011 If Vehicle is a parent and Car is a child then yes, you can't cast Vehicle instance to Car instance. But you probably meant the following use case: class Vehicle { }; class Ferrari : Vehicle { void myUpdate() { } }; Vehicle test = new Ferrari(); Ferrari ferrari = Ferrari(test); Unigine script interpreter supports this type of casting, but it expects the following syntax: Ferrari(test).myUpdate(); Thanks for the information, but it is not working here: namespace DataManager { class ObjectGeneric { }; }; namespace ItemManager { class Item : DataManager::ObjectGeneric { void sayHello() { log.error("Test\n"); } }; }; namespace ItemManager { DataManager::ObjectGeneric temp = new DataManager::ObjectGeneric(); // At this stage it needs to be an ObjectGeneric Item(temp).sayHello(); // Cast to Item class and access method sayHello } Gives me this error: Machine::do_callucfv(): "DataManager::ObjectGeneric 0x7f000e316ea0 (983040:0:0)" is not a suitable user classStack dump: 0x0000: DataManager::ObjectGeneric 0x7f000e316ea0 (983040:0:0) 0x0001: int: 0 Call stack: 00: 0x0000257e ItemManager::receiveSignal() 01: 0x00002645 DataManager::sendSignal() 02: 0x00002655 DataManager::receiveSignal() 03: 0x000027ae MessageManager::dataArrived() 04: 0x000016e9 MessageManager::MessageInterface::checkMessage() 05: 0x000016e2 Disassemble: 0x0000259b: callucfv Item.sayHello 0x00002565 0x0000259f: popucv Item 0x000025a1: pushv managedItemsID World::update(): world update function return 0 Link to comment
ulf.schroeter Posted October 14, 2011 Share Posted October 14, 2011 Just an idea new DataManager::ObjectGeneric() Link to comment
frustum Posted October 14, 2011 Share Posted October 14, 2011 You shouldn't cast user classes. They perform casting automatically. Unigine script generates user class function calls at compile time with according to declared type. class Foo { void foo() { } }; class Bar : Foo { void foo() { } void bar() { } }; // We create new Bar object. They will be passed into the i variable. int i = new Bar(); Bar bar = i; bar.foo(); // compiler knows that bar is "Bar" and generates correct calls. bar.bar(); bar = 12; bar.bar(); // run-time error at this line because bar becomes interger. Foo foo = i; foo.foo(); foo.bar(); // compiler-time error at this line because there is no bar() method in the Foo class. Link to comment
manuel.gysin Posted October 14, 2011 Author Share Posted October 14, 2011 Just an idea new DataManager::ObjectGeneric() Oh the example was bad, I corrected it. But I still can not cast from ObjectGeneric to Item. You shouldn't cast user classes. They perform casting automatically. Unigine script generates user class function calls at compile time with according to declared type. I understand your example, but you never cast from Foo to Bar. In my case I have an ObjectGeneric which I want to cast to Item. In your case this is to cast from Foo to Bar. class Parent { public: void sleep() {} }; class Child: public Parent { public: void gotoSchool(){} }; int main( ) { Parent parent; Child child; // upcast - implicit type cast allowed Parent *pParent = &child; // downcast - explicit type case required Child *pChild = (Child *) &parent; // <---- this is what I want to do pParent -> sleep(); pChild -> gotoSchool(); return 0; } Link to comment
frustum Posted October 15, 2011 Share Posted October 15, 2011 There is no ways to downcast user class, so cast operation isn't required. Link to comment
manuel.gysin Posted October 16, 2011 Author Share Posted October 16, 2011 There is no ways to downcast user class, so cast operation isn't required. Thanks for the information, so I have to validate before creating the object. Link to comment
Recommended Posts