Function Overloading with void in argument bracket of main function [duplicate] - c++

This question already has answers here:
Is there a difference between foo(void) and foo() in C++ or C?
(4 answers)
Closed 3 years ago.
In this code I am overloading the function. But can someone tell me why there is void in the argument brackets of main function. I tried to remove void from the brackets of main function code still works. Any idea ?
#include <iostream.h>
class printData
{
public:
void print(int i)
{
cout << "Printing int: " << i << endl;
}
void print(double f)
{
cout << "Printing float: " << f << endl;
}
void print(char* c)
{
cout << "Printing character: " << c << endl;
}
};
int main(Void)
{
printData pd;
pd.print(5); // Call print to print integer
pd.print(500.263); // Call print to print float
pd.print("Hello C++"); // Call print to print character
return 0;
}

(void) is the list of parameters that main is expecting from its caller. In most cases, its caller is the operating system (or, strictly speaking, the startup code that calls the main function in your program). In C, a void parameter list explicitly states that the function does not expect to receive any parameters from its caller. In C++, a parameter list (void) has the same meaning as an empty parameter list (). Find More Here

Related

How to call a function from a function pointer? [duplicate]

This question already has answers here:
Function pointer as parameter
(3 answers)
Closed 6 months ago.
How can you call a function from only a pointer to the function? For example:
void print()
{
std::cout << "Hello World!" << std::endl;;
}
void run_func(void* func)
{
func(); // what im trying to do (doesnt actually work)
}
int main()
{
run_func(print);
}
Expected output:
Hello World!
It's a bit like how std::thread creates a thread from the pointer of a variable.
Function pointers in function parameter lists need to be wrapped in parentheses. Change the one line, either by wrapping with parentheses or using the typedef'ed function parameter, and your sample works.
// Simplifies arcane function parameter synax.
typedef void (*FUNC_TO_RUN)();
void print()
{
std::cout << "Hello World!" << std::endl;;
}
// Change your parameter as follows. You need to wrap function
// pointers in parantheses.Or, use a typedef.
// void run_func(void *func())
// void run_func(void (*func)()) // Works, but harder to read
void run_func(FUNC_TO_RUN func) // Works, easier to read
{
func(); // what im trying to do (and now works)
}
int main()
{
run_func(print);
}
You have to change your parameter type of run_func to void (*func) (), which means func is a pointer to a void function that takes no parameters.
Corrected code:
void print() {
std::cout << "Hello World!\n";
}
void run_func(void (*func)()) {
func();
// or you can call the print function by (*func)();
}
int main() {
run_func(print);
// prints "Hello World!"
}
You could use std::function:
void print()
{
std::cout << "Hello World!" << std::endl;;
}
void run_func(std::function<void()> f) // f returns void and takes no parameters
{
f();
}
int main()
{
run_func(print);
}

Can't convert GLuint to GLuint() [duplicate]

This question already has answers here:
Why does printing a function name returns a value?
(3 answers)
Closed 3 years ago.
Let's assume the following class Foo.
struct Foo
{
int i;
};
if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor.
int main(void)
{
foo1 = Foo();
cout << foo1.i << " : " << foo1.j << endl; // " 0 "
}
then I thought I'd call the default constructor with the following line but it doesn't:
int main(void)
{
Foo foo2(); // most vexing parse
cout << foo2 << endl; // " 1 " ??? why?
foo2.i++; // error: request for member ‘i’ in ‘foo2’, which is of non-class type ‘Foo()’
}
Why is foo2 initialized to int(1) and not Foo()?
I know about the most vexing parse, it tells that, from my understanding, when a line can be interpreted as a function, it is interpreted as a function.
But foo1() is interpreted as an int, not a function, nor a Foo class.
The line Foo foo2() compiles because it is interpreted as a function prototype. OK.
Why does this line compiles? cout << foo2 << endl;
Does it print the address of the foo2 function?
As you said, Foo foo2(); is a function declaration. For cout << foo2, foo2 would decay to function pointer, and then converts to bool implicitly. As a non-null function pointer, the converted result is true.
Without using boolalpha, the std::basic_ostream<CharT,Traits>::operator<< would output 1 for true.
If boolalpha == 0, then converts v to type int and performs integer output.
You can see it more clearer with usage of std::boolalpha.
cout << boolalpha << foo2 << endl; // print out "true"

Unclear most vexing parse [duplicate]

This question already has answers here:
Why does printing a function name returns a value?
(3 answers)
Closed 3 years ago.
Let's assume the following class Foo.
struct Foo
{
int i;
};
if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor.
int main(void)
{
foo1 = Foo();
cout << foo1.i << " : " << foo1.j << endl; // " 0 "
}
then I thought I'd call the default constructor with the following line but it doesn't:
int main(void)
{
Foo foo2(); // most vexing parse
cout << foo2 << endl; // " 1 " ??? why?
foo2.i++; // error: request for member ‘i’ in ‘foo2’, which is of non-class type ‘Foo()’
}
Why is foo2 initialized to int(1) and not Foo()?
I know about the most vexing parse, it tells that, from my understanding, when a line can be interpreted as a function, it is interpreted as a function.
But foo1() is interpreted as an int, not a function, nor a Foo class.
The line Foo foo2() compiles because it is interpreted as a function prototype. OK.
Why does this line compiles? cout << foo2 << endl;
Does it print the address of the foo2 function?
As you said, Foo foo2(); is a function declaration. For cout << foo2, foo2 would decay to function pointer, and then converts to bool implicitly. As a non-null function pointer, the converted result is true.
Without using boolalpha, the std::basic_ostream<CharT,Traits>::operator<< would output 1 for true.
If boolalpha == 0, then converts v to type int and performs integer output.
You can see it more clearer with usage of std::boolalpha.
cout << boolalpha << foo2 << endl; // print out "true"

Printing the address of member function

struct Widget {
void test() {}
};
int func() {}
int main() {
std::cout << &Widget::test << std::endl;
std::cout << Widget::test << std::endl;
std::cout << func << std::endl;
std::cout << &func << std::endl;
}
In this code only the second line of main function doesn't compile. The others print 1. Why does it print 1. Shouldn't print the address of function? And why second doesn't compile but first does?
Why does it print 1. Shouldn't print the address of function?
No. std::cout can print a void*, but there's no implicit conversion from function pointer types to void* (for neither regular function pointers nor pointer-to-member types). There's a conversion from function pointer types to bool though. That's what we end up with.
And why second doesn't compile but first does?
Because the standard requires you to use & to get the address of a member function.

Error in Function pointers with C++

Why is the following code results in error :
class A {
public:
typedef void (A::*funptr)(void);
void fun(void ) {
cout << " Fun Call " <<endl;
}
void foo(void ) {
cout << " Foo Call " <<endl;
}
funptr p[2];
funptr q;
A()
{
p[0]=&A::foo;
p[1]=&A::fun;
q =&A::fun;
}
};
int main ()
{
A obj;
(obj.*q)(void);
//(obj.p[0])();
//(obj.p[1])();
return 0;
}
You will need to call it like this:
(obj.*obj.q)();
The .* operator doesn't take a member name on the right-hand side, but rather an expression that evaluates to a member pointer. When you write this:
(obj.*q)();
It is looking for a variable called q, but there is no such variable in scope.
Change all occurrences of (void) to (). On the declarations it's redundant, and on the call itself it's not allowed.