Add and test dereference operators

This commit is contained in:
Josh Holtrop 2026-02-23 22:42:43 -05:00
parent 9dfb6e4740
commit be2da870bb
2 changed files with 35 additions and 8 deletions

View File

@ -90,6 +90,11 @@ public:
return ptr; return ptr;
} }
T & operator*() const
{
return *ptr;
}
bool operator!() const bool operator!() const
{ {
return !ptr; return !ptr;

View File

@ -13,6 +13,8 @@ class MyBase
protected: protected:
MyBase(int x, int y) MyBase(int x, int y)
{ {
this->x = x;
this->y = y;
mybase_construct++; mybase_construct++;
} }
@ -20,6 +22,10 @@ protected:
{ {
mybase_destruct++; mybase_destruct++;
} }
public:
int x;
int y;
}; };
class MyDerived : public MyBase class MyDerived : public MyBase
@ -29,6 +35,7 @@ class MyDerived : public MyBase
protected: protected:
MyDerived(double v) : MyBase(1, 2) MyDerived(double v) : MyBase(1, 2)
{ {
this->v = v;
myderived_construct++; myderived_construct++;
} }
@ -36,20 +43,35 @@ protected:
{ {
myderived_destruct++; myderived_destruct++;
} }
public:
double v;
}; };
void t1() void test_destructors_called()
{ {
{
rcp<MyBase> mybase = MyBase::create(4, 5); rcp<MyBase> mybase = MyBase::create(4, 5);
rcp<MyDerived> myderived = MyDerived::create(42.5); rcp<MyDerived> myderived = MyDerived::create(42.5);
} }
int main(int argc, char * argv[])
{
t1();
assert(mybase_construct == 2); assert(mybase_construct == 2);
assert(mybase_destruct == 2); assert(mybase_destruct == 2);
assert(myderived_construct == 1); assert(myderived_construct == 1);
assert(myderived_destruct == 1); assert(myderived_destruct == 1);
}
void test_dereference()
{
rcp<MyBase> mybase = MyBase::create(2, 3);
assert(mybase->x == 2);
assert(mybase->y == 3);
assert((*mybase).x == 2);
assert((*mybase).y == 3);
}
int main(int argc, char * argv[])
{
test_destructors_called();
test_dereference();
return 0; return 0;
} }