---
title: "Fields"
url: https://docs.zeroc.com/ice/3.8/slice/fields
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).

# Fields

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

## Syntax

A field is defined as `Type name`, where `Type` is the field's type and `name` is the field's name. For example:

```slice
string name;
ByteSeq image;
Fruit fruit;
```

The type of a field can be a [basic type](https://docs.zeroc.com/ice/3.8/slice/basic-types/cpp.md), a [user-defined type](https://docs.zeroc.com/ice/3.8/slice/user-defined-types.md), `Object*` (an untyped proxy), or `Value` (an untyped class instance).

## Nullable and Non-Nullable Fields

Fields with proxy and class types are nullable: they can hold null. Fields of other types always hold a value, unless they are [optional fields](#optional-fields) that are not set.

```slice
struct Example
{
    int count; // must be set
    Greeter* greeter; // proxy type, so it can be null
    Node node; // class type, so it can be null
}
```

## Optional Fields

A field of a Slice [class](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/cpp.md) or [exception](https://docs.zeroc.com/ice/3.8/slice/exceptions/cpp.md) may be declared as optional to indicate that a program can leave its value unset. Fields not declared as optional are known as *required* fields.

Each optional field must be assigned a unique, non-negative integer *tag*:

```slice
module M
{
    class C
    {
        string name;
        bool active;
        optional(2) string alternateName;
        optional(5) int overrideCode;
    }
}
```

The scope of a tag is the class or exception that defines the field, and this class or exception only: a derived class or exception can reuse a tag of its base type. For example:

```slice
exception BaseException
{
    optional(1) int systemCode;
}

exception DerivedException extends BaseException
{
    optional(1) string diagnostic; // OK: a different scope
}
```

Optional and required fields can be interleaved, and their tags can be in any order. The tag identifies the field in the [encoding](https://docs.zeroc.com/ice/3.8/encoding/data-encoding-for-optional-values.md); the language mappings do not expose it.

An optional field cannot have a class type, or a constructed type that holds a class type.

## Default Values

You can specify a default value for a field that has one of the following types:

- An [integral](https://docs.zeroc.com/ice/3.8/slice/basic-types/cpp.md) type (`byte`, `short`, `int`, `long`)
- A [floating point](https://docs.zeroc.com/ice/3.8/slice/basic-types/cpp.md) type (`float` or `double`)
- [string](https://docs.zeroc.com/ice/3.8/slice/basic-types/cpp.md)
- [bool](https://docs.zeroc.com/ice/3.8/slice/basic-types/cpp.md)
- [enum](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/cpp.md)

For example:

```slice
struct Location
{
    string name;
    Point pt;
    bool display = true;
    string source = "GPS";
}
```

The legal syntax for literal values is the same as for Slice [constants](https://docs.zeroc.com/ice/3.8/slice/constants-and-literals/cpp.md), and you may also use a constant as a default value. The language mapping guarantees that fields are initialized to their declared default values using a language-specific mechanism.

You can declare a default value for optional fields just as you can for required fields:

```slice
class C
{
    string name;
    bool active = true;
    optional(2) string alternateName;
    optional(5) int overrideCode = -1;
}
```

An optional field with a default value is considered to be set by default.

## Language Mapping

A Slice field maps to a C++ data member with the same name. The type of the C++ data member is the default, memory-owning, mapping of the Slice type.

For example:

```slice
struct Person
{
    string name;
}
```

maps to:

```cpp
struct Person
{
    std::string name; // not std::string_view
    ...
};
```

### Field with a Class Type

A Slice field with a class type maps to a C++ data member with a shared pointer type. For example:

```slice
class Address { ... }

struct Person
{
    string name;
    Address address;
}
```

maps to:

```cpp
class Address { ... };
using AddressPtr = std::shared_ptr<Address>;

struct Person
{
    std::string name;
    AddressPtr address; // can be null
    ...
};
```

### Field with a Proxy Type

A Slice field with a proxy type maps to a C++ data member with a `std::optional<T>` type. For example:

```slice
interface Widget { ... }

struct Person
{
    Widget* favoriteWidgetProxy;
}
```

maps to:

```cpp
class WidgetPrx { ... };

struct Person
{
    std::optional<WidgetPrx> favoriteWidgetProxy; // can be nullopt
    ...
};
```

### Optional Fields {#language-mapping-optional-fields}

An optional field maps to a C++ data member with the same name. The data member's type is the mapped type, wrapped in a `std::optional`.

For example:

```slice
class C
{
    optional(2) string alternateName;
    optional(5) int overrideCode;
    optional(1) Widget* favoriteWidgetProxy;
}
```

maps to:

```cpp
class C
{
    std::optional<std::string> alternateName;
    std::optional<std::int32_t> overrideCode;
    std::optional<WidgetPrx> favoriteWidgetProxy; // single optional
    ...
};
```

Proxies are not wrapped twice in `std::optional`, as illustrated above. As a result, you cannot distinguish between an optional proxy field that is not set and an optional proxy field set to null.

### Default Values {#language-mapping-default-values}

Slice default values map to default member initializers in C++.

For example:

```slice
struct Location
{
    string name;
    Point point;
    bool display = true;
    string source = "GPS";
}
```

maps to:

```cpp
struct Location
{
    std::string name;
    Point point;
    bool display{true};
    std::string source{"GPS"};
    ...
};
```
