copyconstructor.cpp [plain text]
#include <stdio.h>
#include <Block.h>
#include "test.h"
int constructors = 0;
int destructors = 0;
#define CONST const
class TestObject
{
public:
TestObject(CONST TestObject& inObj);
TestObject();
~TestObject();
TestObject& operator=(CONST TestObject& inObj);
int version() CONST { return _version; }
private:
mutable int _version;
};
TestObject::TestObject(CONST TestObject& inObj)
{
++constructors;
_version = inObj._version;
}
TestObject::TestObject()
{
_version = ++constructors;
}
TestObject::~TestObject()
{
++destructors;
}
TestObject& TestObject::operator=(CONST TestObject& inObj)
{
_version = inObj._version;
return *this;
}
void testRoutine() {
TestObject one;
void (^b)(void) __unused = ^{ printf("my const copy of one is %d\n", one.version()); };
}
int main() {
testRoutine();
if (constructors == 0) {
fail("No copy constructors!!!");
}
if (constructors != destructors) {
fail("%d constructors but only %d destructors", constructors, destructors);
return 1;
}
succeed(__FILE__);
}