function without a name?

T

thomas

#include <iostream>
using namespace std;

class bar{
public:
bar(){}
};

class foo{
public:
foo(bar& b){}
foo(int x=0){}
void blah(){
cout<<"blah"<<endl;
}
};

void yourcode(){
foo x(bar()); //error <1>
foo xx(int);
// foo x = foo(bar()); //works
// foo x = bar(); //works
x.blah();
}

int main(){
yourcode();
}
---------------code------------------

FAQ 10.19(http://www.parashift.com/c++-faq-lite/ctors.html) says that
<1> is wrong because:

When the compiler sees Foo x(Bar()), it thinks that the Bar() part is
declaring a non-member function that returns a Bar object, so it
thinks you are declaring the existence of a function called x that
returns a Foo and that takes as a single parameter of type "non-member
function that takes nothing and returns a Bar."

My question is how can "Bar()" declare a function without a name?

For example:

void ();

This declaration is illegal as the compiler complains.
 
V

Victor Bazarov

thomas said:
#include <iostream>
using namespace std;

class bar{
public:
bar(){}
};

class foo{
public:
foo(bar& b){}
foo(int x=0){}
void blah(){
cout<<"blah"<<endl;
}
};

void yourcode(){
foo x(bar()); //error <1>
foo xx(int);
// foo x = foo(bar()); //works
// foo x = bar(); //works
x.blah();
}

int main(){
yourcode();
}
---------------code------------------

FAQ 10.19(http://www.parashift.com/c++-faq-lite/ctors.html) says that
<1> is wrong because:

When the compiler sees Foo x(Bar()),

No, it's when the compiler sees foo x(bar()), it think... C++ is case
sensitive language. Keep this in mind when posting here, please.
> it thinks that the Bar() part is
declaring a non-member function that returns a Bar object, so it
thinks you are declaring the existence of a function called x that
returns a Foo and that takes as a single parameter of type "non-member
function that takes nothing and returns a Bar."

My question is how can "Bar()" declare a function without a name?

I am not sure how to answer your question. How can "for (int i = 0..."
introduce a loop in your program? How can "return" cause the function
to yield control to the caller?
For example:

void ();

This declaration is illegal as the compiler complains.

Yes, it's illegal outside of another declaration. Try this:

foo xx(void());

V
 
T

thomas

My question is how can "Bar()" declare a function without a name?
I am not sure how to answer your question.  How can "for (int i = 0...."
introduce a loop in your program?  How can "return" cause the function
to yield control to the caller?






Yes, it's illegal outside of another declaration.  Try this:

     foo xx(void());

Well, I see. But it causes a warning in VS2005:

warning C4930: 'foo xx(void (__cdecl *)(void))': prototyped function
not called (was a variable definition intended?)

What are declarations like this used for? Any positive effect?
 
V

Victor Bazarov

thomas said:
Well, I see. But it causes a warning in VS2005:

warning C4930: 'foo xx(void (__cdecl *)(void))': prototyped function
not called (was a variable definition intended?)

What are declarations like this used for? Any positive effect?

Uh... If you need to declare a function that takes another function as
its argument (or a pointer to function), then that is a way, although this:

foo xx(void (*pOtherFunc)());

is better (more explicit). It's a style thing. Naming function
arguments is optional in a declaration, but it helps for documenting the
intentions (you may omit the 'pOtherFunc' in the declaration above, but
I suggest keeping it).

If you want to know what function pointers are used for... Well, if you
don't know about them, you most likely don't need them. It's up to you
to decide whether to try to absorb irrelevant information.

Pointers to function are useful when you want to decide at the run time
(i.e. dynamically) what function to call (based on some conditions not
known at compile time). There are numerous examples that you can
probably find on the Web, so I won't bore you with any. Find more about
pointers to functions in your favourite C++ book.

V
 
J

Johannes Schaub (litb)

thomas said:
#include <iostream>
using namespace std;

class bar{
public:
bar(){}
};

class foo{
public:
foo(bar& b){}
foo(int x=0){}
void blah(){
cout<<"blah"<<endl;
}
};

void yourcode(){
foo x(bar()); //error <1>
foo xx(int);
// foo x = foo(bar()); //works
// foo x = bar(); //works
x.blah();
}

int main(){
yourcode();
}
---------------code------------------

FAQ 10.19(http://www.parashift.com/c++-faq-lite/ctors.html) says that
<1> is wrong because:

When the compiler sees Foo x(Bar()), it thinks that the Bar() part is
declaring a non-member function that returns a Bar object, so it
thinks you are declaring the existence of a function called x that
returns a Foo and that takes as a single parameter of type "non-member
function that takes nothing and returns a Bar."

My question is how can "Bar()" declare a function without a name?

For example:

void ();

It may sound weird, but not every declaration needs a name - neither
semantically (i.e syntactically there is something, but it's not the name of
the thiing declared), nor syntactically (i.e also in the syntax, a name is
missing). Example for the syntax case is a function parameter missing a
name. So both of the following declare a parameter, but once without, and
once with a name:

void f(int());
void f(int name());

The semantical case can occur for constructors. These don't have names, but
the name appearing in the declaration just is the class name:

struct A { A(); };

Hope this makes sense.
 
T

thomas

Victor said:
Uh... If you need to declare a function that takes another function as
its argument (or a pointer to function), then that is a way, although this:

foo xx(void (*pOtherFunc)());

is better (more explicit). It's a style thing. Naming function
arguments is optional in a declaration, but it helps for documenting the
intentions (you may omit the 'pOtherFunc' in the declaration above, but
I suggest keeping it).

Good explanation! I think I got it.
----code----
#include <iostream>
using namespace std;

//void doit(int());
void doit(int (*p) ()); //both declarations work

int f(){
cout<<"ok"<<endl; return 0;
}

void g(){
doit(f);
}

void doit(int (*p)()){
(*p)();
}

int main(){
g();
}
--------code-----------
The declaration "void doit(int());" is just like "void doit(int);" in
the style perspective.

If you want to know what function pointers are used for... Well, if you
don't know about them, you most likely don't need them. It's up to you
to decide whether to try to absorb irrelevant information.

Pointers to function are useful when you want to decide at the run time
(i.e. dynamically) what function to call (based on some conditions not
known at compile time). There are numerous examples that you can
probably find on the Web, so I won't bore you with any. Find more about
pointers to functions in your favourite C++ book.

Yeah, thanks for your patience. Actually I know the pointer to
function thing.
But I find it a little difficult to understand C++ in depth just like
the designer views it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top