4 ms·
You can create a class inside a function and return an object of it. That name is not legal outside of the function but its methods can be called.
by germandiago 3y ago
You can create a class inside a function and return an object of it. That name is not legal outside of the function but its methods can be called.
- AnimalMuppet 3y agoAh, I see. Inner to the function, not inner to the class. So the C++ syntax would need to be something like: ClassName::functionName::innerName Except that's not an actual C++ syntax - there isn't one. But if you return it from a function, how do you declare the variable you assign it to? auto? And, is auto a valid return type from a function? If not, how do you declare the function that returns this object?
- germandiago 3y agoExactly. You spell auto or an interface or base class whose derived class cannot be named. I think both are ok. In the latter case you can name the interface instead of auto. Yes, auto is a perfectly valid return type, usually quite useful in generic code. However, in generic code, starting at C++20, I would recommend to use concepts to name a return type that can be many different thing but that is compliant with the same "compile-time" interface. You could do: forward_range auto f(auto && input); You do not spell the return type but you are saying that type conforms to forward_range. Note that the auto type returned could be one of many unrelated types and it is computed at compile-time.