Page 1 of 1

[C++] Deriving from BaseComponent causes using abstract class.

Posted: 25 Apr 2017, 10:47
by realesmedia
When I create class dirived from BaseComponent and INotifyPropertyChanged compiler shows an error "cannot instantiate abstract class). Compiler helper shows I have no overriden functions: GetBaseObject, AddReference, Release, GetNumReferences.
Noesis 2.0.1f1

Re: [C++] Deriving from BaseComponent causes pure virtual class.

Posted: 25 Apr 2017, 11:09
by realesmedia
class BaseDerived : public BaseComponent, public INotifyPropertyChanged {
public:
	virtual ~BaseDerived(){}
	Noesis::PropertyChangedEventHandler& PropertyChanged() {
		return propertyChanged;
	}
protected:
	Noesis::PropertyChangedEventHandler propertyChanged;

	NS_IMPLEMENT_INLINE_REFLECTION(BaseDerived, BaseComponent) {
		NsMeta<TypeId>("BaseDerived");
		NsImpl<INotifyPropertyChanged>();
	}
};

extern "C" NS_DLL_EXPORT void NsRegisterReflection(Noesis::ComponentFactory* factory, NsBool registerComponents) {
	NS_REGISTER_COMPONENT(BaseDerived);
}

Re: [C++] Deriving from BaseComponent causes pure virtual class.

Posted: 25 Apr 2017, 11:22
by realesmedia
Next code can solve compiler errors but I don't know if any errors can occurs.
public:
	virtual BaseObject* GetBaseObject() const { return (BaseObject*)this; }
	virtual NsInt32 AddReference() const { return BaseRefCounted::AddReference(); }
	virtual NsInt32 Release() const { return BaseRefCounted::Release(); }
	virtual NsInt32 GetNumReferences() const { return BaseRefCounted::GetNumReferences(); }

Re: [C++] Deriving from BaseComponent causes using abstract class.

Posted: 25 Apr 2017, 13:04
by sfernandez
Hi,

In C++ we provide the macro NS_IMPLEMENT_INTERFACE_FIXUP to automatically implement those functions. The macro does almost what you posted, but your code will be smaller:
class BaseDerived : public BaseComponent, public INotifyPropertyChanged {
public:
  virtual ~BaseDerived() { }
  Noesis::PropertyChangedEventHandler& PropertyChanged() {
    return propertyChanged;
  }
  NS_IMPLEMENT_INTERFACE_FIXUP
protected:
  Noesis::PropertyChangedEventHandler propertyChanged;
  NS_IMPLEMENT_INLINE_REFLECTION(BaseDerived, BaseComponent) {
    NsMeta<TypeId>("BaseDerived");
    NsImpl<INotifyPropertyChanged>();
  }
};