---
title: "Slice Loaders"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders
language: cpp
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Slice Loaders

Programming language: C++. Other languages: [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/swift.md).

When Ice unmarshals a Slice-defined class or exception, it starts by locating the mapped class and creating an instance of this class.

The abstraction that drives this locate and create process is called the Slice loader. Each communicator has a default Slice loader, implemented by Ice, that locates the generated class for a Slice type ID.

## Custom Slice Loaders

You can install your own custom Slice loader on a communicator to create instances of your own classes, typically classes derived from the generated classes, during unmarshaling. The communicator calls your Slice loader first, and falls back on its default Slice loader when your Slice loader doesn't create an instance.

The communicator calls your Slice loader for both Slice classes and Slice exceptions.

A custom Slice loader derives from `Ice::SliceLoader` and overrides `newClassInstance`, `newExceptionInstance`, or both. You install it by setting the `sliceLoader` field of [InitializationData](https://code.zeroc.com/ice/3.8/api/cpp/structIce_1_1InitializationData.html):

```cpp
class NodeLoader final : public Ice::SliceLoader
{
public:
    [[nodiscard]] Ice::ValuePtr newClassInstance(std::string_view typeId) const final
    {
        return typeId == "::Demo::Node" ? std::make_shared<MyNode>() : nullptr;
    }
};

Ice::InitializationData initData;
initData.sliceLoader = std::make_shared<NodeLoader>();
Ice::CommunicatorPtr communicator = Ice::initialize(initData);
Ice::CommunicatorHolder communicatorHolder{communicator};
```

## See Also

- [Type IDs](https://docs.zeroc.com/ice/3.8/slice/type-ids.md)
