Answers for "declare global variable inside the class c++"

C++
0

how to define global array in c++ in a scope

class C {
 int [] x;

 void method A(int size)
 {
   x = new int[size];   // Allocate the array
   for(int i = 0; i < size; i++)
      x[i] = i;         // Initialise the elements (otherwise they contain random data)
   B();
   delete [] x;         // Don't forget to delete it when you have finished
                        // Note strange syntax - deleting an array needs the []
 }

 void method B()
 {
   int n;
   cin >> n;
   cout << x[n];
   // Be warned, if the user inputs a number < 0 or >= size, 
   // you will get undefined behaviour!
  }
}
Posted by: Guest on September-15-2020
0

c++ variable globale

#include <iostream>

int global = 3; // Une variable globale

void ChangeGlobal()
{
   global = 5; // Référence à la variable globale à l'intérieur d'une fonction
}

int main()
{
   std::cout << global << 'n'; // Référence à la variable globale dans une autre fonction
   ChangeGlobal();
   std::cout << global << 'n';
   return 0;
}
Posted by: Guest on December-24-2020

Code answers related to "declare global variable inside the class c++"

Browse Popular Code Answers by Language