Hidden Constructor - contains a call to A::A() to construct...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Hidden Constructor/Destructor Costs Consider a short example of C++ code: class A { int x, y, z; public: A(); }; class B { A a; public: B() {} }; A::A() {x = 0; y = 0; z = 0;} Class A has a constructor A::A(), used to initialize three of the class's data members. Class B has a constructor declared inline (defined in the body of the class declaration). The constructor is empty. Suppose that we use a lot of B class objects in a program. Each object must be constructed, but we know that the constructor function body is empty. So will there be a performance issue? The answer is possibly "yes", because the constructor body really is NOT empty, but
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: contains a call to A::A() to construct the A object that is part of the B class. Direct constructor calls are not used in C++, but conceptually we could think of B's constructor as containing this code: B::B() {a.A::A();} // construct "a" object in B class There's nothing sneaky about this way of doing things; it falls directly out of the language definition. But in complex cases, such as ones involving multiple levels of inheritance, a seemingly empty constructor or destructor can in fact contain a large amount of processing....
View Full Document

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Ask a homework question - tutors are online