+ 1
What's the use of local class
Hi I just heard about local class defined inside function. Code is tried as below : What's the use of the same ? And how to call the local class function? https://code.sololearn.com/c91PB5xjYgX3/?ref=app
3 Answers
+ 3
This is the first time for me to see that too. I think such class was defined locally because it was meant to only be used locally, where the class was defined.
To call a member function, I guess it's the same way like calling a member function from any class.
LocalClass obj {};
obj.display();
+ 1
Yeah I am also wondering why this provision is there and what's the use case...
I could access its function like normal class but limited to that function defining it... But obviously not from main function or any thing outside the defining function.... What's the need to have class definition inside function
0
One possible used case I can think of is when the local class is extension of another class and implements some vfuncs in there, now you can return a dynamically created object from the function and make the implementation accessable outside the function also ( using base class ptr ).