What's the use of local class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

4th Sep 2021, 6:00 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
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();
4th Sep 2021, 7:29 PM
Ipang
+ 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
4th Sep 2021, 8:05 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
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 ).
6th Sep 2021, 8:47 AM
Arsenic
Arsenic - avatar