Problem with dynamic memory allocation in istream &operator>>

Hi.
I have a question. How do I allocate dynamic memory in istream &operator>>() (operator overloading)? I need to allocate int & char dynamic memory when the program runs. I am an amateur in C++, and if anyone could help me, with the example, it would be great!

My file is as follows:
//header file
#ifndef Money_h
#define Money_h

#include <iostream>
#include <string>
using namespace std;

class Money{
protected:
string billName;
int billCode;
long int refNum;
int accNum;
char *accName;
char *address;
int *startDate;
int *endDate;
int *dueDate;
public:
Money();
Money(int);
Money(int,int,int,int,int,int);
~Money();

friend istream &operator>> (istream &, Money&);
friend ostream &operator<< (ostream &, Money&);

};

#endif

//cpp file
#include <iostream>
#include "Money.h"

using namespace std;

Money::Money()
{
billCode=0, refNum=0, accNum=0, startDate=0, endDate=0, dueDate=0;
}

Money::Money(int size)
{
accName=new char[size], address=new char[size];
}

Money::Money(int bCode, int rNum, int aNum, int sDate, int eDate, int dDate)
{billCode=bCode, refNum=rNum, accNum=aNum, startDate=&sDate, endDate=&eDate, dueDate=&dDate;}

Money::~Money()
{
delete [] accName;
delete [] address;
}

istream &operator>> (istream &is, Money& b){
Money tmp;
tmp.accName=new char[100];//trying to allocate it this way. Is it
tmp.address=new char[100];//possible?
tmp.startDate=new int[100];//
tmp.endDate=new int[100];//
tmp.dueDate=new int[100];//

cout << "Biller name: ";
is >> b.billName;
cout << "Biller code: ";
is >> b.billCode;
cout << "Reference: ";
is >> b.refNum;
cout << "Account number: ";
is >> b.accNum;
cout << "Account name: ";
is >> tmp.accName;
cout << "Address: ";
is >> tmp.address;
cout << "Start date: ";
is >> *tmp.startDate;
cout << "End date";
is >> *tmp.endDate;
cout << "Due date";
is >> *tmp.dueDate;
b=tmp;
return is;
};

ostream &operator<< (ostream &os, Money& b){
return os;
};
Dynamically allocating with new is possible on a pointer, but you do it on a normal integer: int accNum. Here dynamic allocation is explained fully: http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/
Thanks Fransje. I will read through it!
Topic archived. No new replies allowed.