P
Prashant
Okay this is my problem. I have a function, MainDisplay(char*) that
takes in an argument of a string to display. Simple. I want this
function to also be able to display the function that called it. This
is where i'm lost. So for example:
#include <iostream>
using namespace std;
void MainDisplay(char* msg) {
//here 'Function' is the name of the function that MainDisplay
//was called from
cout << Function << " wrote " << msg << endl;
}
int main() {
MainDisplay("Hi");
}
//end
I want to see something like:
main() wrote Hi
I've tried many things. One involved using C macros. For example:
#include <iostream>
using namespace std;
#define DisplayMsg(msg) MainDisplay(__FUNCTION__, msg);
void MainDisplay(char* Function, char* msg) {
cout << Function << " wrote " << msg << endl;
}
int main() {
DisplayMsg("Hi");
}
//end
This has the following output:
main wrote Hi.
This seems to work fine. But SOMETIMES, I get a segmentation fault
when I try to do any io streaming (not limited to cout). I have no
idea why, but people have told me it's because it garbles up some
areas of memory that its not supposed to. So, not being able to
isolate the cause of the problem, I deemed it unsafe and unpredictable
and decided to abandon this method.
Next I tried this:
#include <iostream>
using namespace std;
inline void MainDisplay(char* msg) {
cout << __FUNCTION__ << " wrote " << msg << endl;
}
int main() {
MainDisplay("Hi");
}
//end
I came to this method after hours of searching for a solution. It
said that the code for an inline function gets replaced wherever the
call is made. This would imply that the call MainDisplay("Hi") would
be replaced entirely by
cout << __FUNCTION__ << " wrote " << msg << endl;
but to my dismay the output was:
MainDisplay wrote Hi
Is there a solution to this without using C macros? Is there even a
stable solution at all? Why don't inline functions work like i'm
expecting them to?
Thanks,
Prashant
takes in an argument of a string to display. Simple. I want this
function to also be able to display the function that called it. This
is where i'm lost. So for example:
#include <iostream>
using namespace std;
void MainDisplay(char* msg) {
//here 'Function' is the name of the function that MainDisplay
//was called from
cout << Function << " wrote " << msg << endl;
}
int main() {
MainDisplay("Hi");
}
//end
I want to see something like:
main() wrote Hi
I've tried many things. One involved using C macros. For example:
#include <iostream>
using namespace std;
#define DisplayMsg(msg) MainDisplay(__FUNCTION__, msg);
void MainDisplay(char* Function, char* msg) {
cout << Function << " wrote " << msg << endl;
}
int main() {
DisplayMsg("Hi");
}
//end
This has the following output:
main wrote Hi.
This seems to work fine. But SOMETIMES, I get a segmentation fault
when I try to do any io streaming (not limited to cout). I have no
idea why, but people have told me it's because it garbles up some
areas of memory that its not supposed to. So, not being able to
isolate the cause of the problem, I deemed it unsafe and unpredictable
and decided to abandon this method.
Next I tried this:
#include <iostream>
using namespace std;
inline void MainDisplay(char* msg) {
cout << __FUNCTION__ << " wrote " << msg << endl;
}
int main() {
MainDisplay("Hi");
}
//end
I came to this method after hours of searching for a solution. It
said that the code for an inline function gets replaced wherever the
call is made. This would imply that the call MainDisplay("Hi") would
be replaced entirely by
cout << __FUNCTION__ << " wrote " << msg << endl;
but to my dismay the output was:
MainDisplay wrote Hi
Is there a solution to this without using C macros? Is there even a
stable solution at all? Why don't inline functions work like i'm
expecting them to?
Thanks,
Prashant