3 ms·
To be fair, wasted space was a point many people were making. But it's not the only consideration to make. Anyway, most C++ compilers will turn the following:
by scott_s 16y ago
To be fair, wasted space was a point many people were making. But it's not the only consideration to make.
Anyway, most C++ compilers will turn the following:
obj.method(arg);
Into:
method(&obj, arg);
Conceptually, if not literally. One of the design principles of C++, as stated by Stroustrup, is that you don't pay for what you don't use. So if your classes have no virtual functions and are not a part of an inheritance hierarchy, they should be just plain ol' data. C++ compilers will generate different implementations for different kinds of classes. When virtual members are involved, a C++ object is usually more than just plain ol' data.
But, even in such objects, consider:
class Circle: public Shape {
virtual void draw();
Point coords;
};
In this case, a Circle object will probably not be just POD because it will need to resolve draw() at runtime. But, a Point object will live somewhere in a Circle object - not just a pointer to a Point object. If you wanted that, then you would say:
class Circle: public Shape {
virtual void draw();
Point* coords;
};
And, of course, you would be responsible for managing the dynamic memory for the coords member.