I found in sample code strange syntax. I have no idea what this code does, but looks nothing.
What does syntax type Name(Type) in function means?
void doJob()
{
...
bool SetForward(bool); //strange line
...
}
It's a function declaration.
They don't have to be at file scope, but it's fairly rare to see them inside functions if you're not reading relatively ancient C code.
Function Declarations are usually done at the top of your program so that the main can access all of them !! However , what if you want your main not able to access that function ? You simply declare its prototype in the function which is going to use it !! Another Example can be seen a private function in a class which can only be accessed by class methods & not by the main program !!
Related
I have see codes' example such as ::function() in VS C++ but I cannot uniderstand what does it means.
I understand that :: is used for accessing the member function from outside class, but I have seen examples where I do not find this case. For example in following code
// For painting in memory
class MemCanvas: public Canvas
{
public:
MemCanvas (HDC hdc)
: Canvas (::CreateCompatibleDC (hdc))
{}
~MemCanvas ()
{
::DeleteDC(_hdc);
}
};
See the ::CreateCompatibleDC() function !!
I understand that it is defined in the Windows.h but how it is defined here I cannot guess.
:: refers to the global namespace.
As a prefix for a function call it indicates that this is not a member function, it's a global namespace function.
Together with the naming convention for Windows API functions, it pretty much identifies a Windows API function as such. To the reader. Usually there's no naming conflict so it's not necessary for the compiler: it's just a device to communicate to a reader of the code.
I debug a code, and I use _set_purecall_handler to set function that be called when pure call virtual function happened. This exemple from MSDN work nice for me and do what I want: code from msdn
So, you can see the declaration of the function
void myPurecallHandler(void)
{
printf("In _purecall_handler.");
exit(0);
}
this function MUST return a void value and don't have any arguments, this function is called when a pure call virtuall function happened. I have trying to overload this function to passe it a parameters (The line number where pure call virtuall function happened ), but can't success.
If you see, there is another function there: _set_purecall_handler_m
What is the difference between this function and _set_purecall_handler?
Thanks a lot,
_set_purecall_handler_m is for use with mixed mode CRT when using C++ and C++-CLI. If you are not working with C++-CLI you really don't need to use it. If however you are creating say a DLL that may be used with C++-CLi applications you might want to consider using it.
I have a working python wrapper for C++ code (as suggested here Calling C/C++ from python?
) using ctypes. But the problem is with main function of the code. When I do something like
extern "C" {
void call_main(){ main();}
}
in my c++ code and then call this function via python wrapper
...
lib = cdll.lib('./mylib.so')
def run():
lib.call_main()
-> I get "segmentation fault".
The funny part is that when i copy paste my main method code into function called e.g. test (so it is int test() {....#pasted code...} in c++ code), extern it and then call lib.test()
=> And eveything works fine... So it must be a problem with the main function being called main or something
In C++ calling main() recursively is not allowed ( see 3.6.1, basic.start.main, paragraph 3). Also, you need a C++ aware entry point when you want to call C++ functionality. You can sometimes get away with calling C++ functionality without this but what is going to work and what is not isn't entirely straight forward. The obvious problem is with global objects needing initialization.
Just put the code you want to call into a different function and call this.
have a static function in a header file
class Diagnostics {
public:
static void functionA(){
}
static void functionB(){
some code //works fine until enters the loop below
variable_name // works fine here.
if (condition){ //
variable_name; // after condition is met , i step in here, debugger cannot examine
// the vairable_name which was fine above. right after i try to step over , i get SIGSEV error
some_code; // doesnt even come here. Process exited with SIGSEV
function C(); // tried using classname::functionC , didnt work either
}
}
static void functionC(){
}
static inside a class means that the member or method in question does not operate on an object, i.e. it doesn't define this, but it is still in the class's namespace.
static outside a class means what it means in C: the variable or function does not have external linkage, i.e. things outside the current compilation unit cannot link to it.
Two entirely different things.
I dont know the problem was.
Works fine now. initially happened while I was debugging.
Then i just executed instead of debugging , worked fine.
then i tried debugging again , which worked fine this time.
I'm trying to do a couple of things at once. I'm trying to do one major thing: redirect the application output that is displayed in the Qt Creator console to a textEdit that I have on my GUI. However, the class that I need to do this in is written in C and all of its related headers are in C as well. Is there a way that I can redirect the output into the textEdit within the C class?
If you can modify the C code, you could allow it to take a callback such that text is sent to the callback function, instead of being merely printed with printf. For example, you could have something like:
void someFunctionInC
(
/* other parameters ... */
void (*printcallback)(const char* text, void* extra_arg),
void* extra_arg
)
{
/* ... */
printcallback("Hello world\n",extra_arg); /* instead of using printf */
/* ... */
}
You could then, in C++, create a callback that casts the void* extra_arg parameter back to a class and invokes a method on that class with the given text. Another possibility is you could use snprintf and create a variant of your C function that will print to a string instead of printing to standard out. Note that these solutions all require you to be able to modify the given C function. If it's absolutely not possible to modify the C function, you could use close, pipe, dup2, etc. to redirect stdout to a pipe and then read back the results from the pipe, but that is a really, really ugly solution. Good luck.
See http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.8
Interfacing C++ and C functions involves working around the name mangling that takes place by default in C++ to support function overloading.
To call a C++ function from a C function you must declare the C++ function with extern "C" qualifier. That instructs the compiler to leave its name unmangled.
To call a C function from a C++ function, you must prototype it with extern "C" in the scope of the C++ function.
This is a bit old, but take a look here. It seems like QTextStream is the answer, but the specifics I'm not sure about.
QProcess is QIODevice. If you are invoking this C programm, you may use QProcess and read it's output from it with QProcess::readAll* members