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   | 
		 
		
			
				
				
				  | 
					
						  
						koldo
						 Messages: 3453 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  
 |  
	| 
		
	 | 
 
 
 |  
  
 
Goto Forum:
 
 Current Time: Tue Nov 04 13:59:53 CET 2025 
 Total time taken to generate the page: 0.08323 seconds 
 |