Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

classes, dinamix memory allocation question..

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
hi,
I am reading info from a file, and wishes to read it into
struct. MY question is how do I work with this line
-> blah* PList[100]; What can I do wit that? And lets say I want to copy that into another blah type array, how can ido it?
Also, can we use pointers to get access to private members or functions in a class?
===============================
struct blah
{ int A;
flaot B;
};

class Act
{
private:
blah* PList[100];
int Len;
public:
void ReadFileIn(istream& );
};

void Act::ReadFileIn(istream& Infile )
{ blah* Data;
Data =new blah;

while(Infile)
{
Infile>>Data->A>>Data->B;
PList[Len]=Data;
Len++;
}




=================================================

void main()
{
ifstream Infile("info.txt");
Act Bob;
Bob.ReadFileIn(Infile);

 
First to answer the question:
can we use pointers to get access to private members or functions in a class?
You do not access the private members in your example, however, only the member functions of the class have access to the private data members of the class. To access the private data members, accessors need to be written. These are public member functions e.g.
void setLen(int temp)
{
// set the private data element Len to temp
Len = temp;
}

int getLen()
{
// return the value of Len
return Len;
}

For the coding part of your question this works for me:
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

struct blah
{
int A;
float B;
};

class Act
{
private:
blah* PList[100];
int Len;

public:
void ReadFileIn(char *infile );
};

void Act::ReadFileIn(char *infile )
{
ifstream Infile(infile);

blah* Data = new blah;
Len = 0;

while( !Infile.eof())
{
Infile >> Data->A >> Data->B;
PList[Len] = Data;
Len++;

cout << &quot;A: &quot; << Data->A << &quot; B: &quot; << Data->B << endl;
}
}

void main()
{
char *infile = &quot;info.txt&quot;;

Act Bob;
Bob.ReadFileIn(infile);
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top