System::Object

Base class for all managed System types in sharp-runtime.

include/System/Object.hpp

Status: Implemented

Overview

System::Object mirrors System.Object from .NET and serves as the root of the sharp-runtime type hierarchy. All classes that want runtime type identification or polymorphic equality should inherit from it.

Unlike .NET, Object does not have a garbage collector behind it. Memory management follows standard C++ RAII rules — see Memory Management.

Class Definition

namespace System {
    class Object {
    public:
        virtual ~Object() = default;
        virtual std::string ToString() const;
        virtual bool Equals(const Object& other) const;
        virtual intcs GetHashCode() const;
        virtual std::string GetTypeName() const = 0; // pure virtual
    };
}
Pure Virtual GetTypeName
GetTypeName() is pure virtual — every concrete subclass must implement it. In practice this is done with the GetTypeNameHPP() / GetTypeNameCPP() macros rather than writing the implementation by hand. See Internals: Property Macros.

Methods

ToString()

virtual std::string ToString() const;

Returns a string representation of the object. Default implementation returns the type name. Subclasses should override for meaningful output.

Equals()

virtual bool Equals(const Object& other) const;

Tests equality. Default compares object identity (pointer). Subclasses (e.g., value-type wrappers) override for value equality.

GetHashCode()

virtual intcs GetHashCode() const;

Returns a hash code. Default implementation is based on identity. Subclasses with custom Equals should also override GetHashCode.

GetTypeName()

virtual std::string GetTypeName() const = 0;

Returns the runtime type name as a string (e.g., "System.Int32"). Pure virtual — must be implemented by every subclass.

GetTypeName Macros

Two macros simplify the required boilerplate:

MacroWhere UsedEffect
GetTypeNameHPP().hpp class bodyDeclares the override
GetTypeNameCPP(CLASS, NAME).cpp fileProvides the implementation returning NAME

Example

// In MyClass.hpp
class MyClass : public System::Object {
public:
    GetTypeNameHPP();
};

// In MyClass.cpp
GetTypeNameCPP(MyClass, "MyNamespace.MyClass");

Ownership and Lifetime

Objects are value-managed by default. Pass by reference for polymorphism, or use std::shared_ptr<Object> when shared ownership is needed. There is no garbage collector — all destructors run deterministically at scope end or shared_ptr reference count drop.

Inheritance

The common inheritance pattern:

class MyClass : public System::Object {
public:
    GetTypeNameHPP();
    std::string ToString() const override { return "MyClass"; }
};

For value-type-like wrappers (Int32, Boolean, etc.) the class still inherits Object to retain the polymorphic interface.