Vous êtes sur la page 1sur 2

Classes and Objects in CAPL Page 1 of 2

Topic search

Classes and Objects in CAPL


CAPL Introduction » Classes and Objects in CAPL

In CAPL predefined data types are available that can be used like classes in object-oriented
programming languages such as C++.

This means that functions in the form of methods can be called on variables (objects) of these data
types (classes), and that under certain circumstances destructors can be called automatically to
stop a process and invalidate the variable.

Classes (predefined data types)

Timer, MsTimer
File
TcpSocket
UdpSocket
DiagRequest
DiagResponse
Associative fields
CAN

Method Call

Methods are called on a variable of a class type by writing a point and the method name after the
variable name.

Example

Method call
void myTimer.set(long duration);

Function call
void setTimer (timer myTimer, long duration);

Automatic Destructors

If you declare the variable of a class type within a function or a test case, but not in the global
variables section, a destructor will be called on this variable as soon as the process leaves the
block in which the variable has been declared.

Example
if (someCondition)
{
File file1("C:\\test.TXT",1,0);
// ...
} // file1 is closed here automatically

If you declare the variable of a class type in the global variables section, the destructor will not
be called automatically. You will need to call it explicitly once the object is no longer needed. The

ms-its:C:\Program Files\Vector CANalyzer 9.0\Help01\CANoeCANalyzer.chm::/Topics/S... 1/25/2018


Classes and Objects in CAPL Page 2 of 2

call corresponds to a call to a standard method.

A renewed call of a destructor on a already destructed object has no effect.

Constructors

Most variables of class types which have destructors have to be initialized explicitly before they can
be used. The initialization functions used for this purpose are called constructors.

There are two types of constructors:

Simple constructors
Simple constructors can transfer the necessary parameters directly when a variable is
declared:
file f("C:\\test.TXT", 0, 0);

However, this is not permitted if the variable has been declared in the global variables
section. In this case, the constructor has to be called explicitly:
file f;
f.open("C:\\test.TXT", 0, 0);

Constructor functions
Constructor functions are also used for initialization. Their call is class name::method name:
TestCheck c;
c = TestCheck::CreateTimeout(500);

If a constructor is called again on an object which has already been initialized, the destructor of the
old object is called first and then the variable is initialized.

In the case of constructor functions, the function is evaluated before the old object is destroyed.

Example
TestCheck c;
c = TestCheck::CreateTimeout(500);
// Sequence:
// 1. Call CreateTimeout, i.e. build new check
// 2. destruct old check
// 3. initialize c with the new check
c = TestCheck::CreateTimeout(200);

Note

Variables of the same class type cannot be assigned to one another. However, they
can be transferred to functions as parameters. Please bear in mind that in this case
multiple variables might stand for the same object and that calling a destructor on
one variable will invalidate the other variables.
You can also generate fields from variables of the same class type, but only in the
global variables section. Structures or associative fields cannot contain variables of
a class type.

Data Types for Variables

Version 9.0 SP2


© Vector Informatik GmbH

ms-its:C:\Program Files\Vector CANalyzer 9.0\Help01\CANoeCANalyzer.chm::/Topics/S... 1/25/2018

Vous aimerez peut-être aussi