Reputation: 30963
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();
}
…
…
Upvotes: 2
Views: 303
Reputation: 25537
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 ->
?
Upvotes: 1
Reputation: 101506
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.
Upvotes: 7
Reputation: 3854
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;
Upvotes: 1
Reputation: 355347
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;
Upvotes: 7