Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Access to parent class
Access to parent class [message #52847] Wed, 11 December 2019 10:31 Go to next message
koldo is currently offline  koldo
Messages: 3374
Registered: August 2008
Senior Veteran
Sometimes it is necessary for a child to get access to some data from a parent class. Specially in a multiple TopWindow project, parent pointer cannot be saved in a static location, as there are some instances of it.

A solution could be to give as argument a pointer to the parent, and spread it through all children until the one that needs it.

However I wonder if this could be a finer solution:
template <class T>
T *GetDefinedParent(Ctrl *ths) {
	T *main;
	while (ths->GetParent() != nullptr) {
		ths = ths->GetParent();
		if ((main = dynamic_cast<T*>(ths)) != nullptr)
			return main;
	}
	return nullptr;
}

And used like this from a children:
MyParentClass *parent = GetDefinedParent<MyParentClass>(this);
ASSERT(class);

int neededParameter = ~parent->importantParameter;

What do you think?


Best regards
Iñaki

[Updated on: Wed, 11 December 2019 10:32]

Report message to a moderator

Re: Access to parent class [message #52849 is a reply to message #52847] Thu, 12 December 2019 10:33 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13986
Registered: November 2005
Ultimate Member
koldo wrote on Wed, 11 December 2019 10:31
Sometimes it is necessary for a child to get access to some data from a parent class. Specially in a multiple TopWindow project, parent pointer cannot be saved in a static location, as there are some instances of it.

A solution could be to give as argument a pointer to the parent, and spread it through all children until the one that needs it.

However I wonder if this could be a finer solution:
template <class T>
T *GetDefinedParent(Ctrl *ths) {
	T *main;
	while (ths->GetParent() != nullptr) {
		ths = ths->GetParent();
		if ((main = dynamic_cast<T*>(ths)) != nullptr)
			return main;
	}
	return nullptr;
}

And used like this from a children:
MyParentClass *parent = GetDefinedParent<MyParentClass>(this);
ASSERT(class);

int neededParameter = ~parent->importantParameter;

What do you think?


I am using this approach here and there, but never seemed like worthwhile addition to Core.h...
Re: Access to parent class [message #52850 is a reply to message #52849] Thu, 12 December 2019 13:17 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3374
Registered: August 2008
Senior Veteran
OK, thank you. It is clear this is not perfect.

Best regards
Iñaki
Re: Access to parent class [message #52851 is a reply to message #52850] Thu, 12 December 2019 14:09 Go to previous message
mirek is currently offline  mirek
Messages: 13986
Registered: November 2005
Ultimate Member
On the second thought, if we have things like GetChildCount, we can definitely have this as Ctrl method...

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

struct TestCtrl : Ctrl {
	virtual void LeftDown(Point p, dword keyflags) {
		TopWindow *w = GetAscendant<TopWindow>();
		if(w)
			w->Title(AsString(p));
	}
};

GUI_APP_MAIN
{
	TopWindow win;
	TestCtrl h;
	win << h.SizePos();
	win.Run();
}
Previous Topic: GLDrawDemo NOGTK doesn't work
Next Topic: [SOLVED] Dual GlCtrl in one frame ?
Goto Forum:
  


Current Time: Fri Jun 21 23:29:39 CEST 2024

Total time taken to generate the page: 0.01390 seconds