(e-mail address removed) (ma740988) wrote in message
[ ... ]
class X
{
int Jdx;
friend std:
stream& operator << (std:
stream& os, X const& x);
public:
virtual void std:
stream& Print ( ostream& os ) { os << Jdx; }
}
friend std:
stream& operator << (std:
stream& os, X const& x)
{
return x.Print(os);
}
So far so good?
Yup, reasonable so far -- though this only makes sense if X is going
to (at least potentially) be a base class, in which case its dtor
should usually be virtual as well.
Now heres where this approach gets interesting from the perspective of
an apprentice. For the purposes of discussion, assume class X has 10
member data, three of which is - say - line of sight positions with
units direction cosines.
Occassionally I'm interested in outputting all 10 member data.
Similarily, there are times when I'm interested in outputting only the
line of sight positions.
You have a couple of possibilities here. One would be to have the
class' output function write out all the data, and then have a
function to return the LOS data, which can be written out separately.
Another would be for the LOS data to be stored in a separate class of
its own, and have an instance of that class in the other class.
Without knowing more about what you're doing with it, it's hard to
guess whether that's a good idea though.
Yet another distinctly different possibility would be to create a
manipulator that defines HOW the item will be written out. In a few
cases this can make a lot of sense, but in many other cases it can be
a pretty awful kludge.
[ ... ]
Here's a class I wrote a long time ago to process all the files in a
specified directory tree (that fit a specified mask). The base class
(Search) finds the applicable files. The derived class I've included
simply prints out the full path to each file that was found. Another
derived class might process a file by deleting it, doing a backup on
it, encrypting it, or whatever.
class Search {
// Automates searching a directory (and optionally its subdirectories)
// for a file then processing the file as specified in a derived
class.
//
char *mask_;
char oldpath[FILENAME_MAX];
int doSubdirs_;
public:
// The actual processing to be done on files that are found MUST be
// specified in the derived class.
//
virtual void process(FileEntry *)=0;
void doSearch() {
// The real guts of Search - carries out the actual search.
FileEntry *entry_;
// First look for files in the current directory fitting the
// filename pattern.
//
for (
entry_ = new FileEntry(mask_);
entry_->found();
entry_->next() )
{
process(entry_);
}
delete entry_;
// Then look for directories in the current directory. These
// names do NOT have to fit the filename string.
//
for (
entry_ = new FileEntry("*");
entry_->found();
entry_->next()
) {
if ( entry_->isSubdir()) {
SetCurrentDirectory(entry_->name());
doSearch();
SetCurrentDirectory("..");
}
}
delete entry_;
}
Search(char *mask, int recursive = 1)
: mask_(mask), doSubdirs_(recursive)
{ // Save original directory before starting.
GetCurrentDirectory(FILENAME_MAX, oldpath);
}
~Search() {
// And restore the original directory when we're done.
SetCurrentDirectory(oldpath);
}
};
class Whereis : public Search {
public:
Whereis( char *mask, int recursive = 1)
// By default, a whereis is recursive.
: Search(mask, recursive)
{}
void process(FileEntry *entry ) {
char current[FILENAME_MAX];
GetCurrentDirectory(sizeof(current), current);
// A whereis processes a file by simply printing it out.
std::cout << current << "\\" << *entry << std::endl;
}
};
[ ... ]
This sounds like
vector<int> Vec(5);
copy (Vec.begin(), Vec.end(), ostream_iterator<int>(cout, ' '));
Yes/No?
Sort of, but not really. This is simply taking some existing data and
copying it to another place. I was thinking more along the lines of
something that creates the data, but deposits the output where the
iterator directs it to.
Now how would you map said approach to a class X?
Given what you've described, I probably wouldn't.