C++ Pass Data Type As Argumentative Essay

On By In 1

Pointer Arguments

Pointer Arguments in C Functions

Many functions in external libraries pass arguments by reference. When you pass by reference, you pass a pointer to the value. In the function signature, pointer arguments have names ending in and . Although MATLAB® does not support passing by reference, you can create a MATLAB argument, called a lib.pointer object, that is compatible with a C pointer. This object is an instance of the MATLAB class.

Often, you can simply pass a MATLAB variable (passing an argument by value), even when the signature for that function declares the argument to be a pointer. There are times, however, when it is useful to pass a .

  • You want to modify the data in the input arguments.

  • You are passing large amounts of data, and you want to control when MATLAB makes copies of the data.

  • The library stores and uses the pointer so you want the MATLAB function to control the lifetime of the object.

Put String into Void Pointer

C represents characters as 8-bit integers. To use a MATLAB character array as an input argument, convert the string to the proper type and create a . For example:

The syntax creates the null-terminated string required by the C function. To read the string, and verify the pointer type, enter:

MATLAB automatically converts an argument passed by value into an argument passed by reference when the external function prototype defines the argument as a pointer. Call a function that takes a to a string as an input argument using the following syntax.

Although MATLAB converts the argument from a value to a pointer, it must be of the correct type.

str = 'string variable'; vp = libpointer('voidPtr',[int8(str) 0]);
char(vp.Value) vp.DataType
ans = string variable ans = voidPtr

Memory Allocation for External Library

In general, MATLAB passes a valid memory address each time you pass a variable to a library function. Use a object in cases where the library stores the pointer and accesses the buffer over time. In these cases, ensure that MATLAB has control over the lifetime of the buffer and prevent copies of the data from being made. The following pseudo-code is an example of asynchronous data acquisition that shows how to use a in this situation.

Suppose an external library has the following functions:

AcquireData(int points,short *buffer) IsAquisitionDone(void)

where is declared as follows:

First, create a to an array of 99 points:

BufferSize = 99; pBuffer = libpointer('int16Ptr',zeros(BufferSize,1));

Then, begin acquiring data and wait in a loop until it is done:

calllib('myLib','AcquireData,BufferSize,pbuffer) while (~calllib('myLib','IsAcquisitionDone') pause(0.1) end

The following statement reads the data in the buffer:

When the library is done with the buffer, clear the MATLAB variable:

See Also

Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.

Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.

The concept of templates can be used in two different ways:

  • Function Templates
  • Class Templates

Function Templates

A function template works in a similar to a normal function, with one key difference.

A single function template can work with different data types at once but, a single normal function can only work with one set of data types.

Normally, if you need to perform identical operations on two or more types of data, you use function overloading to create two functions with the required function declaration.

However, a better approach would be to use function templates because you can perform the same task writing less and maintainable code.


How to declare a function template?

A function template starts with the keyword template followed by template parameter/s inside   which is followed by function declaration.

template <class T> T someFunction(T arg) { ... .. ... }

In the above code, is a template argument that accepts different data types (int, float), and class is a keyword.

You can also use keyword instead of class in the above example.

When, an argument of a data type is passed to , compiler generates a new version of  for the given data type.


Example 1: Function Template to find the largest number

Program to display largest among two numbers using function templates.

Output

In the above program, a function template is defined that accepts two arguments and of data type . signifies that argument can be of any data type.

function returns the largest among the two arguments using a simple conditional operation.

Inside the function, variables of three different data types: , and are declared. The variables are then passed to the function template as normal functions.

During run-time, when an integer is passed to the template function, compiler knows it has to generate a function to accept the int arguments and does so.

Similarly, when floating-point data and char data are passed, it knows the argument data types and generates the function accordingly.

This way, using only a single function template replaced three identical normal functions and made your code maintainable.


Example 2: Swap Data Using Function Templates

Program to swap data using function templates.

Output

Before passing data to function template. i1 = 1 i2 = 2 f1 = 1.1 f2 = 2.2 c1 = a c2 = b After passing data to function template. i1 = 2 i2 = 1 f1 = 2.2 f2 = 1.1 c1 = b c2 = a

In this program, instead of calling a function by passing a value, a call by reference is issued.

The function template takes two arguments and swaps them by reference.


Class Templates

Like function templates, you can also create class templates for generic class operations.

Sometimes, you need a class implementation that is same for all classes, only the data types used are different.

Normally, you would need to create a different class for each data type OR create different member variables and functions within a single class.

This will unnecessarily bloat your code base and will be hard to maintain, as a change is one class/function should be performed on all classes/functions.

However, class templates make it easy to reuse the same code for all data types.


How to declare a class template?

template <class T> class className { ... .. ... public: T var; T someOperation(T arg); ... .. ... };

In the above declaration, is the template argument which is a placeholder for the data type used.

Inside the class body, a member variable and a member function are both of type .


How to create a class template object?

To create a class template object, you need to define the data type inside a when creation.

className<dataType> classObject;

For example:

className<int> classObject; className<float> classObject; className<string> classObject;

Example 3: Simple calculator using Class template

Program to add, subtract, multiply and divide two numbers using class template

Output

In the above program, a class template is declared.

The class contains two private members of type : & , and a constructor to initalize the members.

It also contains public member functions to calculate the addition, subtraction, multiplication and division of the numbers which return the value of data type defined by the user. Likewise, a function to display the final output to the screen.

In the function, two different objects and are created for data types: and respectively. The values are initialized using the constructor.

Notice we use and while creating the objects. These tell the compiler the data type used for the class creation.

This creates a class definition each for and , which are then used accordingly.

Then, of both objects is called which performs the Calculator operations and displays the output.

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *