realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

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

25 Apr 2017, 10:47

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
Last edited by realesmedia on 25 Apr 2017, 15:21, edited 2 times in total.
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

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

25 Apr 2017, 11:09

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);
}
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

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

25 Apr 2017, 11:22

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(); }
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

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

25 Apr 2017, 13:04

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>();
  }
};

Who is online

Users browsing this forum: Semrush [Bot] and 16 guests