|
|
|
|
|
by gpderetta
1098 days ago
|
|
In an OO model, you can implement exactly that by having FILE and DIR derive from a common base class, then you can have your function take such a base and either act on the common subset or do a type query and cast to the derived type as needed (this is usually frowned on, but not uncommon). But yes, of course shoving everything under OO is bad. A bit of OO should be a mean to an end, not a inflexible design principle. |
|
Ah, but now you're changing the problem :) we are not in C++, we do not have base classes, we are in C and have just two so-called objects. They don't belong to us, they're exposed as part of an API.
To recap, objects are entities that bind together data and functions. You can't have a single function belong to two objects, by definition.
Some people say that FILE and DIR plus the associated functions are objects; even though there's no syntax for it, they are objects conceptually, it's supposedly the same thing.
But if FILE and DIR are 2 conceptual/imaginary objects, to which of the objects does F(FILE, DIR) belong?
And, given they're not real objects, the answer can be whatever you want it to be.
You can say that in this scenario they stop being objects at all, or you can say FILE owns the function because it's first (but then about the encapsulation, why does F use the internals of both), or you can say conceptual objects can be conjoined like siamese twins, or whatever you feel like it. You can say the set of all functions that take an int is an object. Once you go down this road, you can say anything is OOP if you look at it in a certain light.
I understand that people can/do apply the OOP mindset to anything, it's their choice. But I don't think it's fair to characterize C as OOP since it doesn't even have syntax for objects. Syntax is real, people's mental models are not. But that's just me.