---
name: "autoptr"
section: "3bobcat"
platform: "linux"
package: "libbobcat-dev"
version: "2.08.01-1"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/autoptr"
---

# autoptr(3bobcat)

a generalization of std::auto\_ptr

## NAME

FBB::AutoPtr - a generalization of std::auto\_ptr

## SYNOPSIS

**#include \<bobcat/autoptr\>**\

## DESCRIPTION

The class **AutoPtr** implements a generalization of the class *std::auto\_ptr*. Like **std::auto\_ptr** ab() can be used to handle a pointer to an allocated data element, but unline bd(std::auto\_ptr) **AutoPtr** can also be used to handle a pointer to an array of data elements. Moreover, **AutoPtr** supports non-destructive assignment copy- and move construction, therefore allowing **AutoPtr** objects to be stored in, e.g., abstract containers. The class uses reference counting to share data among multiple objects.

Since almost all operators and members provide access to (mutable) data stored in the **AutoPtr** object, no members return or expect *const* references, pointers or **AutoPtr** objects.

## NAMESPACE

**FBB**\
 All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace **FBB**.

## INHERITS FROM

**FBB:RefCount**

## CONSTRUCTORS

- o

  **AutoPtr\<Type\>()**:\
   The default constructor, initializing its data element pointer  to a 0-pointer to a *Type* data type.

-

  - o

  **explicit AutoPtr\<Type\>(Type \*data)**:\
   The constructor initializing its data element pointer to *data*. The *data* pointer must have been dynamically allocated and is owned by the **AutoPtr** object.

-

  - o

  **AutoPtr\<Type\>(Type \*data, size\_t size)**:\
   The constructor initializing its data element pointer to *data*, which is an array of *size* *Data* elements. The *data* pointer must have been dynamically allocated and is owned by the **AutoPtr** object.

- o

  *AutoPtr\<Type\>* offers copy and move constructors.

## OVERLOADED OPERATORS

Some of the following operators return references or pointers to data elements owned by the **AutoPtr** object. It should be noted that, since reference counting is used for the data pointer and not for the data itself, modifying a data element using one **AutoPtr** object will usually also modify the data element of other **AutoPtr** objects sharing the object’s data pointer.

- o

  **AutoPtr\<Type\> &operator=(AutoPtr\<Type\> &other)**:\
   The assignment operator assigns the information referred to by *other* to the current **AutoPtr** using reference counting.

- o

  **Type &operator\*()**:\
   The dereference operator returns a reference to the data element pointed to by the **AutoPtr**. If the **AutoPtr** object points to an array of *Type* elements, a reference to its first element is returned.

- o

  **Type \*operator-\>()**:\
   The pointer to member operator returns a pointer to the data element pointed to by the **AutoPtr**. If the **AutoPtr** object points to an array of *Type* elements, a pointer to its first element is returned.

- o

  **Type &operator\[\](int idx)**:\
   The index operator returns a reference to the element *idx*  owned  by the **AutoPtr**. No array-bound checking is performed.

- o

  **AutoPtr\<Type\> operator+(int idx)**:\
   The plus operator returns an **AutoPtr** object whose dereference operator will return element *idx* of the current **AutoPtr** object, allowing users to apply pointer arithmetic. The returned **AutoPtr** object shares the data with the current object, but uses an offset value of its own to specify its own first element. Consequently, **AutoPtr**’s pointer arithmetic operators may be nested.

- o

  **AutoPtr\<Type\> &operator+=(int idx)**:\
   The plus arithmetic assignment operator returns the current **AutoPtr** object whose dereference operator is incremented by *idx*.

- o

  **AutoPtr\<Type\> operator-(int idx)**:\
   The minus operator returns an **AutoPtr** object whose dereference operator will return element *-idx* of the current **AutoPtr** object, allowing users to apply pointer arithmetic. The returned **AutoPtr** object shares the data with the current object, but uses an offset value of its own to specify its own first element. Consequently, **AutoPtr**’s pointer arithmetic operators may be nested.

- o

  **AutoPtr\<Type\> &operator+=(int idx)**:\
   The subtract arithmetic assignment operator returns the current **AutoPtr** object whose dereference operator is decremented by *idx*.

## PUBLIC MEMBER FUNCTIONS

All members of **std::ostringstream** and   **std::exception** are available, as **FBB::AutoPtr** inherits from these classes.

Some of the following members return references or pointers to data elements owned by the **AutoPtr** object. It should be noted that, since reference counting is used for the data pointer and not for the data itself, modifying a data element using one **AutoPtr** object will usually also modify the data element of other **AutoPtr** objects sharing the object’s data pointer.

- o

  **Type \*get()**:\
   The **get()** member returns a pointer to the data element pointed to by the **AutoPtr**. If the **AutoPtr** object points to an array of *Type* elements, a pointer to that array’s first element is returned.

- o

  **Type \*release()**:\
   The **release()** member returns a pointer to the data element pointed to by the **AutoPtr**. If the **AutoPtr** object points to an array of *Type* elements, a pointer to its first element is returned. The returned pointer is owned by **release()**’s caller and the current object’s data pointer is set to zero. If the data were shared among several **AutoPtr** objects, the remaining objects will keep their data. In that case the returned pointer is a pointer to a copy of the shared data. The *Type* data type must support a copy constructor.

- o

  **Type \*releaseAll()**:\
   The **releaseAll()** member returns a pointer to the data element pointed to by the **AutoPtr**. If the **AutoPtr** object points to an array of *Type* elements, a pointer to its first element is returned. The returned pointer is owned by **releaseAll()**’s caller. The data pointers of all **AutoPtr** objects that shared the returned data become zero-pointers. if the **AutoPtr** object contains a pointer to an array of *Type* objects, the returned pointer will also point to such an array. The *Type* data type must support a copy constructor.

## FREE OVERLOADED OPERATOR

Note that the following operator is defined in the *FBB* namespace. Koenig lookup is used to select it whenever the appropriate types of arguments are provided to the plus operator.

- o

  **AutoPtr\<Type\> FBB::operator+(int idx, AutoPtr\<Type\> &rhs)**:\
   This free (classless) plus operator returns an **AutoPtr** object whose dereference operator will return element *idx* of the *rhs* **AutoPtr** object, allowing users to apply pointer arithmetic. The returned **AutoPtr** object shares the data with the *rhs* object, but uses an offset value of its own to specify its own first element. Consequently, **AutoPtr**’s pointer arithmetic operators may be nested.

## EXAMPLE

```
    #include "../autoptr"
    using namespace std;
    using namespace FBB;
    int main()
    {
        AutoPtr<int> autoInt(new int(4));
        cout << "\n1: test simple construction and copy construction\n\n";
        {
            AutoPtr<int> auto2(autoInt);
            cout << *auto2 << endl;
            cout << *((0 + auto2) - 2) << endl;
        }
        cout << *autoInt << "\n" <<
                "\n2: test array\n\n";
        {
            AutoPtr<int> autoarr(new int[5], 5);
            autoarr[1] = 1;
            autoarr[2] = 2;
            cout << autoarr[2] << "\n" <<
                    autoarr[3] << endl;
        cout << "\n3: assign to former autoPtr of different data size\n\n";
            autoInt = autoarr;
        }
        cout << autoInt[2] << "\n" <<
                autoInt[3] << endl;
        cout << "\n4: pointer-based operations\n\n";
            cout << *((autoInt + 4) - 2) << "\n" <<
                    *((autoInt + 6) - 4) << "\n" <<
                    ((6 + autoInt) - 4)[0] << endl;
        cout << "\n5: resetting\n\n";
            AutoPtr<int> autoSave(autoInt);
            autoInt.reset(new int(12345));
            cout << *autoInt << "\n" <<
                    autoSave[2] << endl;
        cout << "\n6: resetting all\n\n";
            autoSave = autoInt;
            autoInt.resetAll(new int(12345));
            cout << *autoInt << "\n" <<
                    *autoSave << endl;
    }
```

## FILES

*bobcat/autoptr* - defines the class interface

## SEE ALSO

[**bobcat**(7)](/bobcat/7.md), **RefCount**(3bobcat)

## BUGS

None Reported.

## DISTRIBUTION FILES

- o

  *bobcat\_2.08.01-x.dsc*: detached signature;

- o

  *bobcat\_2.08.01-x.tar.gz*: source archive;

- o

  *bobcat\_2.08.01-x\_i386.changes*: change log;

- o

  *libbobcat1\_2.08.01-x\_\*.deb*: debian package holding the libraries;

- o

  *libbobcat1-dev\_2.08.01-x\_\*.deb*: debian package holding the libraries, headers and manual pages;

- o

  *http://sourceforge.net/projects/bobcat*: public archive location;

## BOBCAT

Bobcat is an acronym of \`Brokken’s Own Base Classes And Templates’.

## COPYRIGHT

This is free software, distributed under the terms of the  GNU General Public License (GPL).

## AUTHOR

Frank B. Brokken (**f.b.brokken@rug.nl**).
