In article <11**********************@v46g2000cwv.googlegroups .com>,
"JoeC" <en*****@yahoo.com> wrote:
I am writing a program and I would like to load a graphic file to a
constructor. The graphic stuff shouldn't matter, I want to pass a file
name to a constructor and create an object with the data in a
particular file so far I have:
graphic::graphic(std::string n){
std::ifstream fin("graphic.txt"); <-- I want to pass a string then
load that data into an object.
std::ifstream fin(n); <-- How can I make this work?
Others have answered your specific questions so I'm going to make a more
general critique...
I'm not sure that such a constructor is a good idea. Not all data comes
from files after all. Better might be something like this:
graphic::graphic( istream& is ) {
// use 'is' like you would 'fin'
}
Now you can load from things other than files, as long as they derive
from istream. Also, you can keep several graphics in one file and pass
it from one graphic object to another. This would come in handy for your
animation class (if you end up with one.)
class animation {
std::list< graphic > graphics;
public:
animation::animation( istream& is ) {
while ( is ) {
graphics.push_back( graphic( is ) );
}
}
};
Of course, animation and graphic should probably derive from some common
ABC as well...
Just some thoughts...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.