problem with getItemDataAt() and Any's


25-12-2008 18:07:06

void LevelEditor::itemSelected(MyGUI::WidgetPtr w, size_t _index) {
Objects::Object* obj = static_cast<MyGUI::ListPtr>(w)->getItemDataAt<Objects::Object>(_index);

I am getting this error:
error C2259: 'Spelprj::Objects::Object' : cannot instantiate abstract class
1> due to following members:
1> 'const Ogre::String &Spelprj::Objects::Object::getTypeName(void)' : is abstract
1> c:\development\gameproject\include\spelprj_object.h(74) : see declaration of 'Spelprj::Objects::Object::getTypeName'
1> c:\development\sdk\mygui\include\mygui_any.h(137) : see reference to class template instantiation 'MyGUI::Any::Holder<ValueType>' being compiled
1> with
1> [
1> ValueType=Spelprj::Objects::Object
1> ]

Am I misusing the method or is it that it can't handle abstract classes/objects?

Thanks in advance!


26-12-2008 16:39:37

Any class always contains the copy of object. You need to write there pointer to object. And destroy object yourself by that pointer ;) .

26-12-2008 16:45:08

/** @example "Class Any usage"
void f()
// RU: тестовый класс, с простыми типами все аналогично
// EN: test class, with simple types all is similar
struct Data { int value; };

// RU: экземпляр и инициализация
// EN: instance and initialization
Data data;
data.value = 0xDEAD;

// RU: создастся копия класса Data
// EN: copy of class Data will be created
MyGUI::Any any = data;
// RU: копия класса Data
// EN: copy of class Data
Data copy_data = *any.castType<Data>();
// RU: теперь value == 0xDEAD
// EN: now value == 0xDEAD
int value = copy_data.value;

// RU: создастся копия указателя на класс Data
// EN: copy of pointer on class Data will be created
any = &data;
// RU: копия указателя на класс Data и конкретно на объект data
// EN: copy of pointer on class Data and on object data
Data * copy_ptr = *any.castType<Data*>();
// RU: теперь data.value == 0
// EN: now value == 0
copy_ptr->value = 0;



26-12-2008 17:47:29

So what's wrong with this line?
Objects::Object* obj = static_cast<MyGUI::ListPtr>(w)->getItemDataAt<Objects::Object>(_index);

PS. Does the Any posses a copy of the object? If so, it means that I will have 2 copies of an object instead of just use a pointer?


26-12-2008 18:28:07

You should store there pointer to Object instead Object and then Objects::Object* obj = *static_cast<MyGUI::ListPtr>(w)->getItemDataAt<Objects::Object*>(_index);because Any can store only types with value semantic and your Object is abstract type.


27-12-2008 20:57:48

Thanks, it works fine now!