What is an example of using C ++ pointers?

I learn C ++ myself and I understand how pointers work. but the document I am using is quite literal, and the examples do not really affect why or when the pointers will be used. a couple of real-world examples will help me maintain knowledge.

+3
source share
10 answers

You use pointers when you want your objects to exist longer than the current stack. You can also use them to avoid copying objects to containers.

// TODO: Remember to call DeleteObjects() when you're done here!!
std::vector<MyObject*> Objects;

void Test()
{
    MyObject *const pObject = new MyObject();
    Objects.push_back(pObject);
}

void DeleteObjects()
{
    std::vector<MyObject*>::iterator it = Objects.begin(), itEnd = Objects.end();
    for (; it != itEnd; ++it)
    {
        delete *it;
    }
    Objects.clear();
}
+9
source

, , , , . , , ( ), .

, , , node , 0 ( NULL, ), . ( ++ - y, )

struct TreeNode
{
  TreeNode* left;
  TreeNode* right;
}

, , .

+3
  • . ,
  • . .
  • : , .
+1

, . , . . , - . "", , .

+1

++, C. C

+1
+1
void print_values(int* iptr, int size)
{
    int i;
    for (i=0; i < size; i++)
    {
        printf("%d ", *(iptr++));
    }
    printf("\n");
}

int main()
{
    int table[256];
    memset(table, 0, sizeof(table));
    print_values(table, sizeof(table)/sizeof(int));
}

():

#define ___FUNC_B { func_1, func_2, func3 }
void ( __closure __fastcall * __FUNC_B [__FUNC_MAX] )( TObject* ) = ___FUNC_B;

:

CClass *ptr = new CClass();
/* something */
delete ptr;

, , , - (, sort), :

vector <CClass*> Vptr;
for (i=0; i < 100; i++)
{
    Vptr.push_back(new CClass());
}

sort(Vptr.begin(), Vptr.end(), __cmp_func);
/* something */

while (!Vptr.empty())
{
    delete *(Vptr.begin());
    Vptr.erase(Vptr.begin());
}

C:

char *memory = (char*) malloc(1024);
if (memory == NULL)
{
    exit(1);
}

/* you have alocated 1KB in memory */
memory = (char*) realloc(2048);
if (memory == NULL)
{
    exit(1);
}

/* you have alocated 2KB in memory */
free(memory);
/* you have nothing */
0

++ ?

:

  • . , , C, ++ . , .

  • . , , .

  • () ( ).

  • , ( ).

  • ( , ..). , , , , , .

. , , , ( ), , () int/- . API, , (. WinAPI HANDLE, HWND ..) - , - - , ).

0

, , , .

: ++. C .

RAII:

// BAD
void func(size_t n)
{
  int* array = new int[n];
  // .. use array
  delete[] array;
}

// GOOD
void func(size_t n)
{
  std::vector<int> array(n, 0);
  // .. use array
}

: delete, . , new , , - .

// BAD: Contract: do not pass NULL
void func(int* i);

// GOOD
void func(int& i);

, NULL ( ), (const).

:

void printSorted(std::vector<BigType> const& values)
{
  std::vector<BigType*> references = from(values);

  std::sort(references.begin(), references.end(), ByPointer());

  std::transform(references.begin(), references.end(),
                 std::ostream_iterator<BigType>(std::cout, " "),
                 Dereference());
}

Object* find(Key const& key);

:

boost::optional<Object&> find(Key const& key);

.

clone Boost Cloneable:

struct Base
{
  virtual ~Base() {}

  virtual Base* clone() const = 0;
};

: . :

struct Derived: Base
{
  virtual Derived* clone() const { return new Derived(*this); }
};

, Derived const&, , , , - Derived.

, , Smart Containers:

std::unique_ptr<Base> u = derived.clone();
0

, , .

, , , . , , .

, , :

int* drawer = 0;

, 0 , , , ? , :

int value = *drawer;

( ):

*drawer = 15;

, , , , , , :

int* drawer = 0;
int* drawer_copy = drawer;
*drawer = 15;

? "drawer_copy", 0 "", 15.

, "&" , :

int value = 15;
int* drawer = &value;

:

value = 5;

"* drawer" 5.

As you can see, pointers allow the user to have more control over memory and have reserved memory forever, once you have declared a pointer, you can save the address and access it whenever you want. :)

-1
source

Source: https://habr.com/ru/post/1752800/


All Articles