Lets consider the next piece of code:
#include <iostream>
void print()
{
std::cout << "I feel void" << std::endl;
}
void (*func)();
func = print;
This does not compile, since "func does not name a type".
But I already declared about func's type. It's a function pointer that takes no arguments and returns void. Why do I need to name a type again?
Do I always have to initialize a function pointer when declaring it?
No.
However, you cannot have expression statements such as assignments in namespace scope.
you can do that in main! , you cant do that in namespaces:
void (*func)();
void print()
{
std::cout << "I feel void" << std::endl;
}
int main(int argc, char* argv[])
{
func = print;
return 0;
}
but if you want do that before main execution , you can try below code(I prefer dont use this , but it's helpful sometimes):
#include <iostream>
void (*func)();
void print()
{
std::cout << "I feel void" << std::endl;
}
class Initilizer
{
public:
Initilizer()
{
init();
}
static void init()
{
func = ::print;
}
};
Initilizer obj;
int main(int argc, char* argv[])
{
func(); /*Test func*/
return 0;
}
Related
Consider the below code :
#include <iostream>
void test_function(const char* caller_name = __FUNCTION__) {
std::cout << caller_name << " ";
}
void func1() {
test_function();
}
int main() {
test_function();
func1();
}
The above code prints nothing.
Is there any way that test_function prints the name of the function from which it is called ?
Eg : For this case, output should be : main func1
I understand that desired output can be achieved if we make the argument to test_function, non default, and pass __FUNCTION__ from the caller function.
This could be achieved by using macro also, but it is not what I am looking for.
Use a macro
#define TEST_FUNCTION() test_function(__FUNCTION__)
void test_function(const char *caller) {
std::cout << caller << '\n';
}
void foo() {
TEST_FUNCTION(); // prints foo
}
int main() {
TEST_FUNCTION(); // prints main
foo();
}
I built an interface taking pointers to functions. Sometimes this calculation depends on state, which I want to encapsulate in a class and pass its method:
#include <iostream>
class Printer {
public:
static void print(int i) { // Want to get rid of the static
std::cout << i << "\n";
}
};
template<typename int_func>
void with_1(int_func func) {
func(1);
}
int main(int argc, char const *argv[]) {
Printer printer;
with_1(printer.print);
return 0;
}
I need non-static methods (and would even prefer overloading operator()). However removing the static results in error: a pointer to a bound function may only be used to call the function.
I could use a dummy like this:
Printer printer;
void dummy(int i) {
printer.print(i);
}
int main(int argc, char const *argv[]) {
with_1(dummy);
return 0;
}
But that does not look elegant to me. Can I write a template that accepts both, function pointers and non-static methods? Or is there even a better design pattern for my problem?
You can not simply pass non-static method like this, because they work on instance. A simply solution is to use lambda:
#include <iostream>
class Printer {
public:
static void print(int i) { // Want to get rid of the static
std::cout << i << "\n";
}
};
template<typename int_func>
void with_1(int_func func) {
func(1);
}
int main(int argc, char const *argv[]) {
Printer printer;
// Can use capture by reference because we are sure printer still
// exist during execution of with_1
with_1([&printer](int i){ printer.print(i); });
return 0;
}
example
Try this:
int main(int argc, char const *argv[]) {
Printer printer;
with_1( std::bind( &Printer::print, printer, std::placeholders::_1 ) );
return 0;
}
(You'll need to #include <functional>.)
The program:
#include <iostream>
void foo(void (*bar)()){ bar(); };
void foo(int a = 5)
{
std::cout << a << std::endl;
}
int main()
{
foo(foo); //Error
}
DEMO
I expected that eventually foo(5) will be called. In contrast, the following program works fine:
#include <iostream>
void foo(void (*bar)()){ bar(); };
void foo()
{
std::cout << 5 << std::endl;
}
int main()
{
foo(foo); //OK
}
DEMO
Could you explain that difference?
In the first example although foo has default argument its type is void (bar*)(int). Having the default argument makes it possible to call foo without specifying the argument value explicitly, but there is still an int argument. Simply its value is automatically populated(during compilation).
This code is not working
any idea why
#include <QCoreApplication>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "a.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
boost::asio::io_service io;
std::cout << "Wait for five seconds\n";
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
// t.wait();
A *vvv=new A();
std::cout << "Hello, world!\n";
t.async_wait(&print);
std::cout << "Keep cool and wait!\n";
return a.exec();
}
void print(const boost::system::error_code& /*e*/)
{
std::cout << "You are cool!\n";
}
Error:
D:\qtsrc\asiotry\main.cpp:14: error: C2065: 'print' : undeclared
identifier
Have a look at this code:
int *address = &i; //using i here!
int i; //declaration
What is wrong with this code? You're doing something similar.
Declare print before main() so you can use it in main():
//declaration
void print(const boost::system::error_code& /*e*/);
int main()
{
//use print here
}
//definition
void print(const boost::system::error_code& /*e*/)
{
std::cout << "You are cool!\n";
}
Note that all you're required to declare the function before its usage, that does not necessarily mean that the declaration must be above main(). You could as well do this also:
int main()
{
//declaration
void print(const boost::system::error_code& /*e*/);
//use print here AFTER the declaration!
}
Hope that helps you to understand the concept of declaration and usage. -)
Remember one thing. The declaration of a function should precede its reference(usage) in the program.
Either add this before main:-
void print(const boost::system::error_code& /*e*/);
OR
Define the function before main.
It's because when you use print in the async_wait call, it's not declared yet, so it doesn't know what print is.
There are two possible solutions to this:
Move the definition of the print function to above main.
Before using it, declare it as a prototype.
#include <boost/bind.hpp>
#include <iostream>
using namespace std;
using boost::bind;
class A {
public:
void print(string &s) {
cout << s.c_str() << endl;
}
};
typedef void (*callback)();
class B {
public:
void set_callback(callback cb) {
m_cb = cb;
}
void do_callback() {
m_cb();
}
private:
callback m_cb;
};
void main() {
A a;
B b;
string s("message");
b.set_callback(bind(A::print, &a, s));
b.do_callback();
}
So what I'm trying to do is to have the print method of A stream "message" to cout when b's callback is activated. I'm getting an unexpected number of arguments error from msvc10. I'm sure this is super noob basic and I'm sorry in advance.
replace typedef void (*callback)(); with typedef boost::function<void()> callback;
A bound function doesn't produce an ordinary function, so you cannot just store it in a regular function pointer. However, boost::function is able to handle anything as long as it is callable with the correct signature, so that's what you want. It will work with a function pointer, or a functor created with bind.
After a few corrections to your code, I came up with this:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
// i prefer explicit namespaces, but that's a matter of preference
class A {
public:
// prefer const refs to regular refs unless you need to modify the argument!
void print(const std::string &s) {
// no need for .c_str() here, cout knows how to output a std::string just fine :-)
std::cout << s << std::endl;
}
};
// holds any arity 0 callable "thing" which returns void
typedef boost::function<void()> callback;
class B {
public:
void set_callback(callback cb) {
m_cb = cb;
}
void do_callback() {
m_cb();
}
private:
callback m_cb;
};
void regular_function() {
std::cout << "regular!" << std::endl;
}
// the return type for main is int, never anything else
// however, in c++, you may omit the "return 0;" from main (and only main)
// which will have the same effect as if you had a "return 0;" as the last line
// of main
int main() {
A a;
B b;
std::string s("message");
// you forget the "&" here before A::print!
b.set_callback(boost::bind(&A::print, &a, s));
b.do_callback();
// this will work for regular function pointers too, yay!
b.set_callback(regular_function);
b.do_callback();
}