K
K
Kirill H2016-09-25 18:42:21
C++ / C#
Kirill H, 2016-09-25 18:42:21

How to convert an array of pointers to an array of C++ references?

Everything is complicated :)
There is a class Manager and FracNum. Instances of the FracNum class are created in an array of pointers to them in the Manager class. If the array is too small to contain the next instance of the FracNum class, then the function is triggered and the array is doubled.
An array of pointers is used for the addition() function; for the function subtraction(); links must be used.
How can this be done? Need to create another dynamic array for references in exactly the same way as an array of pointers?
Some code:
manager.h

class FracNum;

class manager {
private:

  int SizeCounter;
  int FracNumCounter;
  
  FracNum * mCurrentFracNumPointer;
  FracNum * parray;
  FracNum * bufarray;

  void resizeArray();

public:
  
  manager() : SizeCounter(0), FracNumCounter(0), mCurrentFracNumPointer(0), parray(0)
  {

    SizeCounter = 3;

    printf("Logs:\nSize ");
    printf("%d\n", SizeCounter);
    printf("FracNums ");
    printf("%d\n", FracNumCounter);

    parray = new FracNum[SizeCounter];
    mCurrentFracNumPointer = parray;

    printf("Array create success...\n");
  }
  
  ~manager()
  {
    
  }	
};

manager.cpp
void manager::resizeArray()
{
  //Creating buf array
  printf("\nCreate temp array.");
  FracNum * bufarray = new FracNum[SizeCounter];
  //Coppy elements to buf array
  printf("\nCopying elements from old to new array...");
  for (int i = 0; i < FracNumCounter; i++) { bufarray[i] = parray[i]; }
  //Clear old array
  printf("\nDestroy old array.\n");
  delete[] parray;
  //New derection XD
  printf("\nRedirection pointing to new place in memory.\n");
  parray = bufarray;
}

void manager::NewFracNum(int Numerator, int Denominator, string xSign)
{
  // in this case we can assign array
  if (FracNumCounter == SizeCounter)
  {
    cout << endl << "---SOURCE PROCESS-----------------------------------------------------";
    printf("\nDetected that current array isn't big enough to accept all FracNums.\n");
    printf("\nDoubled size...");
    SizeCounter *= 2;

    printf("\nNew size is %d", SizeCounter);

    resizeArray();
  }

  bool Sign;
  if (xSign == "+" || xSign == "1" || xSign == "true") {
    Sign = true;
  }
  if (xSign == "-" || xSign == "0" || xSign == "false") {
    Sign = false;
  }

  *(parray + FracNumCounter++) = FracNum(Numerator, Denominator, Sign);

  printf("\nSystem:\nNew FracNum will be created.\nCreated FracNums & Curren size of array:\t %d | %d\n", FracNumCounter, SizeCounter);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AtomKrieg, 2016-09-25
@KirillHelm

you don't need to start anything, outwardly links are slightly different from ordinary objects. It is enough to dereference pointers to use them as a parameter at the link: https://ideone.com/GoFyGs
or rewrite the function to accept the same types

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question