function without a name?

Discussion in 'C++' started by thomas, Oct 22, 2009.

  1. thomas

    thomas Guest

    #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.
     
    thomas, Oct 22, 2009
    #1
    1. Advertising

  2. thomas wrote:
    > #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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 22, 2009
    #2
    1. Advertising

  3. thomas

    thomas Guest

    > > 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());
    >


    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?
     
    thomas, Oct 22, 2009
    #3
  4. thomas wrote:
    >>> 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());
    >>

    >
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 22, 2009
    #4
  5. thomas wrote:

    > #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.
     
    Johannes Schaub (litb), Oct 22, 2009
    #5
  6. thomas

    thomas Guest

    Victor Bazarov wrote:
    > thomas wrote:
    > >>> 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());
    > >>

    > >
    > > 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).
    >


    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.
     
    thomas, Oct 22, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey
    Replies:
    0
    Views:
    349
  2. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    827
    Roedy Green
    Jan 2, 2006
  3. ding feng
    Replies:
    2
    Views:
    2,819
    ding feng
    Jun 25, 2003
  4. SanPy
    Replies:
    6
    Views:
    845
    Gabriel Genellina
    Oct 3, 2007
  5. Patrick

    Getting windows user-name without domain name

    Patrick, Dec 7, 2007, in forum: ASP .Net Security
    Replies:
    2
    Views:
    541
    Nikolay Podkolzin
    Dec 14, 2007
Loading...

Share This Page