What does it means if I declare function pointer in c++ - c++

What does it means when I in my code declare something like :
Foo * foo();
What benefits it gives me ?
Ist not simple pointer variable as you see .
Also
What does it mean if I declare it in the c++ source above all methods for example :
// Test cpp class
.
#include "Foo.h"
Foo * foo();
Test::Test() {}
Test::GetFooMethods()
{
foo()->someMethod();
}
…
…

Foo * foo(); is a declaration to a function named foo which returns a pointer to a Foo object. It is not a function pointer.
The code Foo()->someMethod(); is probably a typo. You probably meant foo()->someMethod();. In this case, the code calls foo() which as stated above returns a pointer to a Foo object, and then you call the Foo::someMethod() method through that pointer.
If in fact you did mean Foo()->someMethod(); things get a little crazy. What this means is first a Foo object is default initialized on the stack. Then Foo::operator->() is called, returning some object which has a method named someMethod(), and that function is called. See why I guess that it was a typo? :)
EDIT:
To answer the last question in your post, when you declare a function without defining it, you are telling the compiler, "I've got a function named foo that takes no parameters and returns a pointer to a Foo, but I'm not going to tell you how that function is implemented yet." This allows you to write code that uses that function and the compiler will be happy. The linker however will be quite grumpy with you if you do not someplace provide a definition for this function.

Foo * foo();
This is not a function pointer. This is a declaration of a function named foo that takes no arguments and returns a pointer to a Foo.
A pointer to a function of this type would look like:
Foo* (*foo_ptr)() = foo;

Just like
int getX();
is a declaration of a function that returns an int, so too
Foo * foo();
is a declaration of a function that returns a Foo pointer;

What does it means when I in my code
declare something like : Foo * foo();
Foo * foo();
Very tough to say without knowing what is Foo.
If Foo is a type e.g. struct Foo{};, then this declares a function named 'foo' which returns a pointer to some type called Foo. This looks to be the case in your post based on your usage subsequently (and also the title of the post).
If Foo is a #define e.g. #define Foo 1, then this is not a declaration but an expression statement which multiplies 1 by the return value of 'foo' in case operator * is defined for the types of the operands involved.
What benefits it gives me ?
Tough to say again without knowing what Foo is.
Also What does it mean if I declare it
in the c++ source above all methods
Declaring 'foo' (if it is indeed a declaration) as high as possible in the translation unit means larger the tentative scope of the name 'foo'.
void f1(){g();} // not OK, 'g' not known
void f(){}
void g(){f();} // OK, f() is known
BTW, does Foo (caps F) have an overloaded operator ->?

Related

Why can we use a forward declared function but not instantiate a forward declared class?

While reading this post I was wondering why calling a function previously forward-declarated is possible like in this example
int f(int x, int y); // forward declaration
int main()
{
return f(2,3);
}
int f(int x, int y)
{
return x + y;
}
but instantiating or calling a member variable of a class previously forward-declared is not possible like in the example
class Foo; // forward declaration
int main()
{
Foo foo;
return 0;
}
class Foo
{
int x = 3;
};
My thaughts are that a function is just like an address, which is defined when the forward declaration is done (for example f() is assigned 0xABC). Then when parsing the return f(x,y) line the compiler just injects that address 0xABC in the binary code and then later when parsing the declaration of the function the compiler start inputting the instruction starting from address 0xABC.
However, when parsing the class Foo forward declaration if the compiler assigns to that class to some memory say 0xDEF, then when it parses Foo foo it will not know how much space to allocate since all the members of the class were not defined yet so the compiler doesn't know how much space to allocate in total
I don't really know anything about compilers, so is this correct?
Your assumptions are mostly correct, but you don't need to think about how compilers work.
By forward declaring a function, you make its signature available to the rest of the code. This means its name, return value, and parameter types are known, and that's all you need syntactically to call that function.
By forward declaring a class, you only make it known that its name refers to a type. That enables you to use it in various contexts. For example, you can declare (but not define) a function with that class as a parameter type or as the return type. Or you can define a pointer to that class, because the type a pointer points to doesn't matter as long as you don't try to dereference it. Or you can use it as argument to a typename in a template, as long as that template doesn't actually use it (but it could define a pointer to it or declare a function with it as parameter type and so on).
Knowing only that some particular name refers to a class, you cannot deduce anything about its contents (and yes, its size) or how to actually use it.

'this' pointer changes after calling a member function

I use Visual Studio 2012 Express to debug a 64-bit app. Let's say both 'foo' and 'bar' are some member functions of a class C.
'foo' looks like:
void foo() {
bar(); // change to this->bar() works!
}
My program crashed because 'this' pointer is changed when it went inside 'bar'. The problem can be fixed by changing to 'this->bar()'.
Any idea how I should debug this problem? Thanks
You mean that the following code works?
void foo() {
this->bar(); // instead of bar()
}
So, presuming foo() is a member function that calls another member function bar() for the same object, check two things: (1) whether there is another non-member function bar() having same signature of member function bar(), if so exist, change the non-member function name op always qualify with this-> for member function call as a good programming practice (2) When you call foo() for some object instance or pointer to object instance, check whether the object has been allocated and initialized properly.

Typedef and function declaration

I'm trying to write:
typedef int foo();
...
foo bar
{
return 1;
}
But I have error C2206, that typedef cannot be using for function declaration. What causes this error? I think foo bar should just compile as int bar().
This declaration:
typedef int foo();
is perfectly valid. It defines foo as an alias for the type int(), or "function returning int and taking no arguments".
(An aside: C uses (void) to indicate no arguments; C++ uses (). An earlier version of this answer was based on C rules.)
It's more common to define typedefs for pointer-to-function types rather than for function types, but both are valid. Given the above typedef, you could declare a function pointer object as:
foo *funcptr;
But you can't declare or define a function using a typedef. If the language permitted it, you could declare a function as:
foo func; /* would be equivalent to "int func(void);" *if* it were legal */
or:
foo func { return 42; }; /* would be equivalent to
"int func(void) { return 42; }"
*if* it were legal */
There are even times when this would be convenient. The standard signal function, defined in <signal.h> or <csignal>, has a fairly complicated declaration:
void (*signal(int sig, void (*func)(int)))(int);
You can define a typedef for the function type of a signal handler:
typedef void (signal_handler)(int);
would would allow signal to be declared as:
void (*signal(int sig, signal_handler *func);
but you can't use that typedef to declare or define your own signal handler -- though you can usefully use it to declare pointers to signal handler functions:
signal_handler *funcptr = my_handler;
signal(SIGINT, funcptr);
The same applies to the function pointer arguments required by qsort() and bsearch().
So given that you can define a typedef for a function type, why can't you use it to declare or define a function?
I don't think there's any deep reason. The language just doesn't happen to permit it. For function declarations, one problem is that
signal_handler foo;
looks just like an object declaration, but it would instead declare a function (that must be defined elsewhere). And permitting typedefs for function definitions:
signal_handler foo { /* ... */ }
would require a change to the language grammar -- and would make it difficult to see the parameter and return types when looking at the definition. Also, function definitions, unlike declarations, require names for any parameters, which would add another level of complexity.
But the real reason, I think, is that Dennis Ritchie, when he designed C, either didn't think of it, or didn't think it was worthwhile (and Stroustrup didn't have a good enough reason to change that when he designed C++).
The declaration for a function pointer typedef is
typedef int (*foo)();
Also you are missing parens in your function declaration
foo bar()
{
return 1;
}
Also 1 is not a legal function pointer value.
foo bar()
{
return 1; // this line is an error
}

How to call member functions from their address

I have a function that resides in a class in an application my objective is to inject a dll into the target proccess and call that member function by its address.
Here is the function:
void GameAudio::StopAllSounds(void) // this is at address 0x004A0656
I have tried calling it like this
typedef void(__stdcall * caller)(void);
caller call = (caller)0x004A0656;
I have used everything: __stdcall , __cdecl, __thiscall you name it.
Class's non-static member function signature is different than the normal function.
For example,
class X {
public:
void foo ();
};
void foo ();
In above case X::foo() and ::foo() are different from each other. Remember that, X::foo() under the hood is somewaht like:
void X::foo (X* const this);
^^^^^ implicitly added
Thus what you are attempting to do would result in an undefined behavior.
If you insists to call the member function using its address, then better to make it a static member function.
class X {
public:
static void foo ();
};
void foo ();
Signature for X::foo() and ::foo() are same.
Edit: From your comment, it seems that you don't have the control over the source code. I would suggest to use the proper calling convention for the function signature instead of hard coded address. Pseudo code
typedef void (GameAudio::*caller_type)();
caller_type caller = &GameAudio::StopAllSounds;
GameAudio object;
(object.*caller)();
There is no syntax in C++ for what you are doing.
Note that the function could even be virtual, and so you'd be calling the wrong function.
The easiest way is to change it/ into/add a non member function, or a static member function, with an additional GameAudio* parameter. That function could be easily be called using a pointer-to function.
If you still want to call the member function directly your best bet is probably to use assembler. The problem then is the portability: your solution will depend on the compiler, SO, architecture and maybe even the ABI version.

C++ void prefixed to a function call. eg. `main() {void func();}`

void func() {assert(0);}
int main () {void func();}
The above code does not call func(), or at least does not reach the assertion. Not that I really need to know, but I'm just curious, what is going on here?
You're declaring a prototype for a function named func which returns nothing and takes no arguments. That's (one of) the subtle difference between function calls and function prototypes. Notice that the line above main, the void func() {assert(0);}, has no effect on whether this is a prototype or a call. You could remove it and the code would do the same thing - that is, nothing.
This also tells you that you can redeclare function prototypes. You could even have this:
int main() {
void blah();
void blah();
void blah();
void blah();
}
And the code would still do what it did before - nothing.
If you leave off the void, it would call the function.
Also, notice that in the case of a function which takes parameters, this:
int main() { func(4); }
would not turn into a prototype if you added void before it like this:
int main() { void func(4); }
it would just produce a syntax error.
As others have pointed out, the line
void func();
inside of main is treated as a function prototype rather than a call to the function func. In C and C++, you can declare function prototypes inside of functions if you wish, though it's rarely done in practice.
The fact that this is legal causes all sorts of headaches for programmers. For example, if you rewrote the code as
(void) func();
Then this would compile as a call to func whose return type is explicitly casted to void to indicate "I don't care about this return value." In other words, this set of parentheses changes the declaration into a statement.
In C++, this problem can be compounded by the fact that this code below is a function prototype, not a variable declaration invoking the default constructor:
Object myObject();
Though
Object myObject(137);
does create the object and pass 137 into its constructor, and
Object myObject;
creates the object without calling the constructor.
There is an awful edge case of the language called the "most vexing parse" that arises when trying to declare an object while calling its constructor. For example, this code is legal C++, but it's a function declaration rather than a variable declaration:
set<int> mySet(istream_iterator<int>(cin), istream_iterator<int>());
The problem is that this could be parsed as a function declaration rather than a creation of an object that accepts two temporary istream_iterator<int>s as parameters. To fix this, in C++ you'd have to write
set<int> mySet(istream_iterator<int>(cin), (istream_iterator<int>()));
Where, as above, the extra parentheses forcibly disambiguate the statement from being a function prototype to being a declaration.
Hope this helps!
You can declare functions, even when it's unnecessary. That's what you've done, re-declared the function.
You are declaring a local function void func() inside main().
The void statement indicates the compiler that it is a declaration and not a function call. So, remove the void, your function will be called.