class Mesh
{
public:
Mesh(PolygonClass& a_PolygonClass) { PolygonList.push_back(a_PolygonClass); }
}
Is this better/faster to have the constructor defined in a header file like so
or defined in a separate .cpp file when the constructor is called?
Also on another question. Is it conventional to always create a constructor for a class? Does it increase performance when you do it like i have done it before?
1 2 3 4 5 6 7 8
class Object
{
public:
void Init();
};
Object A;
A.Init();
It can be faster when it is in the header, because it becomes eligible for inlining.
If it's in the source file, it can generally only be inlined if link-time optimization is enabled.
RMA wrote:
Is it conventional to always create a constructor for a class? Does it increase performance when you do it like i have done it before?
No. A constructor doesn't have anything to do with performance. You add one if you need one and you don't when you don't. If you have no constructor, the compiler will automatically create one that calls the standard constructor of all members.
If you'd remove the constructor from your Mesh class, then yes, Mesh instances could be created faster, because the push_back doesn't need to be executed anymore. Will the program still do what you want? Probably not.
Edit: ah, I only now registered your second snippet.
No, it wouldn't increase performance. The compiler will create a default constructor anyway, so you don't gain anything from calling Init from outside the class. However, it violates RAII, which results in a lot of problems.