If I have a class that inherits from a base class:
1 2 3 4 5 6 7 8 9
struct Base {
int g;
);
struct Subclass1 : public Base {
int some_data1;
};
struct Subclass2 : public Base {
int some_data2;
}; // just a basic example, I know it looks unnecessary to use inheritance here
I know the whole point of inheritance is avoid having to duplicate unnecessary stuff, but I'm just wondering if there is some implicit conversion, Sub -> Base, that happens when I call a function such as:
1 2 3 4
void foo(const Base& b1)
{
std::cout << b1.g;
}
as opposed to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void foo(const Subclass1& s)
{
// duplicated body
std::cout << s.g;
}
void foo(const Subclass2& s)
{
// duplicated body
std::cout << s.g;
}
// in other words:
template <typename T>
void foo(const T& base_or_sub)
{
std::cout << base_or_sub.g;
}
1 2 3 4 5 6 7
int main() {
Base b;
Subclass1 s1;
Sublcass2 s2;
foo(b);
foo(s1);
foo(s2);
or is that resolved at compile time, so no overhead?
Edit: Well whether there's overhead or not, it's probably minimal, and friend GraphicArray operator+(const GraphicArray&, const GraphicArray&);
looks a hell of a lot better than
> GraphicArray operator+(const GraphicArray&, const GraphicArray&);
¿you are returning a `GraphicArray' object or a derived type?
check out object slicing.
I am returning the base type object. I think I solved it my edit by doing friend GraphicArray operator+(const GraphicArray&, const GraphicArray&); instead of templates, but yes I am still curious.
In my edited example, Curve2D is declared as Curve2D : public GraphicArray { ... };
Thanks for mentioning object slicing, I wasn't sure what the name for passing a subclass into the base class parameter was called.
I am still confused though, does the program make more overhead (internal operations/copying?) to slice the object when passing a reference of it, or would it be the same as if the function wanted the Subclass directly as one of its parameters?
you do not observe object slicing when passing a reference.
you observe it when trying to construct a base object from a derived object. There are no means to copy the extra members that the derived object may have.
> I am returning the base type object.
I don't know how are you defining the "sum" operation, but I doubt that a base class object may represent correctly the result when the parameters needed derived types
> does the program make more overhead
I don't know, but I doubt that that is the case.
You are simply treating it as if it were a Base&, and the compiler just have to analyse the relationships to allow it. No operations are needed.
Okay thank you, I was thinking that it would need to adjust things at runtime to account for passing a subclass object, but it makes sense that the compiler would analyze the functions beforehand.
And just for information: I am only using the base class's member variables in my sum operation. For example, I have two "specialized" GraphicArrays (Curve class), which have their unique stuff in them, but if I were to add two "curves" together, it is no longer a well-defined curve, so the returned object is the vertices + other rendering information of the two combine curves, with no knowledge that those vertices represent curved parametric functions.