Scalar

A scalar is a variable that holds one value at a time. This section describes how to create, access, and modify scalars, along with examples.

Creating Scalars

Scalars created in the following examples are stored in ConstantSP. The basic data type definitions are declared in the header files <ScalarImp.h> and <ConstantImp.h>.

The DolphinDB C++ API provides two methods for creating scalars:

(1) Use new expression.

ConstantSP i = new Int(1);                          //create a scalar of type Int with value 1
ConstantSP s = new String("DolphinDB");             //create a scalar of type String with value "DolphinDB"
ConstantSP t = new Date(2022, 11, 1);               //create a scalar of type Date with value 2022.11.1
ConstantSP d = new Double(1.2);                     //create a scalar of type Double with value 1.2

(2) Use functions provided in the header <Util.h>.

The above scalars can also be created by the following script.
ConstantSP i = Util::createInt(1);
ConstantSP s = Util::createString("DolphinDB");
ConstantSP t = Util::createDate(2022, 11, 1);
ConstantSP d = Util::createDouble(1.2);

Accessing Scalars

To access a scalar, use the corresponding get function. For detailed data type mappings, see Data Types and Forms.

Note: The getString() function can be applied to data objects of any data type to convert their values to strings.

int ci          = i->getInt();
std::string cs  = s->getString();
int cd          = t->getInt();         //The Date values in C++ are stored as int. 
double cv       = d->getDouble();

Updating Scalars

To update a scalar, use the corresponding set function. For detailed data type mappings, see Data Types and Forms.

i->setInt(2);
s->setString("C++ API");
t->setInt(5000);              //Number of days to January 1, 1970
d->setDouble(2.3);