C++ Function Return a Pointer

Description

C++ Function Return a Pointer

#include <iostream> 
#include <sstream> 
#include <stdlib.h> 

using namespace std; 

string *readCode() /*from w ww .j  a  v  a  2s. c  o m*/
{ 
  string *code = new string; 
  code->append("asdf"); 

  int randomnumber = rand(); 
  ostringstream converter; 
  converter << randomnumber; 

  code->append(converter.str()); 
  code->append(" asdf"); 

  return code; 
} 

int main() 
{ 
  string *newcode; 
  int index; 

  for (index = 0; index < 10; index++) 
  { 
    newcode = readCode(); 
    cout << *newcode << endl; 
  } 

  return 0; 
}



PreviousNext

Related