R
Robbie Hatley
I've got a function that I use a lot when making utility programs
that need to do the same thing to every directory in a tree.
Its prototype is:
unsigned long int CursDirs (void Func(void));
This just applies the fuction Func to every subdirectory of the
current directory. It works fine when I pass it pointers to regular
void-void functions.
But last night I had a situation where it would be very handy to pass
it a pointer to a member function of a class. This is in a program
that counts lines of text in all *.c, *.cpp, and *.h files in a
directory tree, starting at current directory and recursively descending.
(Basically, "line count of this project".) In this program, I have
this class:
class SourceTree
{
public:
SourceTree(void) : Lines(0UL) {}
void CountLines(void);
void PrintLines(void);
private:
void CountLinesInFile (const std::string& FileName);
void CountLinesInCurDir (void);
unsigned long int Lines;
};
I tried to pass &SourceTree::CountLinesInCurDir to CursDirs:
SourceTree::CountLines(void)
{
CursDirs(&SourceTree::CountLinesInCurDir);
return;
}
but OOPS, that's not allowed!
So I tried to use mem_fun from <functional> like this:
SourceTree::CountLines(void)
{
CursDirs(mem_fun(&SourceTree::CountLinesInCurDir));
return;
}
But that doesn't work either. I get an error message saying
"cannot convert std::mem_fun_t<void, SourceTree> to void(*)()"
What am I doing wrong here? Is there a way to get mem_fun to
work in this application, or is this too far removed from it's
original intended purpose (ie, std. algorithms)?
(In the mean time, I had to copy&paste the entire body of CursDirs
into CountLines. This works, but is very messy. It'd sure be nice
to be able to do it in one line instead.)
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
that need to do the same thing to every directory in a tree.
Its prototype is:
unsigned long int CursDirs (void Func(void));
This just applies the fuction Func to every subdirectory of the
current directory. It works fine when I pass it pointers to regular
void-void functions.
But last night I had a situation where it would be very handy to pass
it a pointer to a member function of a class. This is in a program
that counts lines of text in all *.c, *.cpp, and *.h files in a
directory tree, starting at current directory and recursively descending.
(Basically, "line count of this project".) In this program, I have
this class:
class SourceTree
{
public:
SourceTree(void) : Lines(0UL) {}
void CountLines(void);
void PrintLines(void);
private:
void CountLinesInFile (const std::string& FileName);
void CountLinesInCurDir (void);
unsigned long int Lines;
};
I tried to pass &SourceTree::CountLinesInCurDir to CursDirs:
SourceTree::CountLines(void)
{
CursDirs(&SourceTree::CountLinesInCurDir);
return;
}
but OOPS, that's not allowed!
So I tried to use mem_fun from <functional> like this:
SourceTree::CountLines(void)
{
CursDirs(mem_fun(&SourceTree::CountLinesInCurDir));
return;
}
But that doesn't work either. I get an error message saying
"cannot convert std::mem_fun_t<void, SourceTree> to void(*)()"
What am I doing wrong here? Is there a way to get mem_fun to
work in this application, or is this too far removed from it's
original intended purpose (ie, std. algorithms)?
(In the mean time, I had to copy&paste the entire body of CursDirs
into CountLines. This works, but is very messy. It'd sure be nice
to be able to do it in one line instead.)
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant