struct foo and function foo !?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Razvan

    struct foo and function foo !?

    Hi !



    Today I saw some code like this:

    struct foo {
    foo(foo* s2){
    cout << "foo::foo";
    }
    };

    foo* foo(foo *s2)
    {
    cout << "foo::(foo* )";
    return s2;
    }

    The name of the struct is 'foo' and the name of the
    function is 'foo' also ! Why they are not conflicting ? The name of
    types and the names of functions (and varialbles) are kept in separate
    places ? Is this standard behaviour ?



    Regards,
    Razvan
  • Razvan

    #2
    Re: struct foo and function foo !?

    Tough question ?!!
    Maybe somebody can pinpoint me to the right direction.

    Razvan



    mihai11@mailcit y.com (Razvan) wrote in message news:<15f19d61. 0406260828.7cc5 5@posting.googl e.com>...[color=blue]
    > Hi !
    >
    >
    >
    > Today I saw some code like this:
    >
    > struct foo {
    > foo(foo* s2){
    > cout << "foo::foo";
    > }
    > };
    >
    > foo* foo(foo *s2)
    > {
    > cout << "foo::(foo* )";
    > return s2;
    > }
    >
    > The name of the struct is 'foo' and the name of the
    > function is 'foo' also ! Why they are not conflicting ? The name of
    > types and the names of functions (and varialbles) are kept in separate
    > places ? Is this standard behaviour ?
    >
    >
    >
    > Regards,
    > Razvan[/color]

    Comment

    • Prateek R Karandikar

      #3
      Re: struct foo and function foo !?

      > > Today I saw some code like this:[color=blue][color=green]
      > >
      > > struct foo {
      > > foo(foo* s2){
      > > cout << "foo::foo";
      > > }
      > > };
      > >
      > > foo* foo(foo *s2)
      > > {
      > > cout << "foo::(foo* )";
      > > return s2;
      > > }
      > >
      > > The name of the struct is 'foo' and the name of the
      > > function is 'foo' also ! Why they are not conflicting ? The name of
      > > types and the names of functions (and varialbles) are kept in separate
      > > places ? Is this standard behaviour ?[/color][/color]

      Yes, unfotunately this is Standard behaviour. A struct and a function
      in the same scope can have the same name. It is one of the unwanted
      leftovers of the C language. In C, foo wouldn't be a type, struct foo
      would be a type, so there was no ambiguity. In C++ now foo itself is a
      type, but the committee chose not to break C compatibility in this
      case. However it is best to avoid this "feature". A good compiler
      should give a warning.

      -- --
      Abstraction is selective ignorance.
      -Andrew Koenig
      -- --

      Comment

      • Denis Remezov

        #4
        Re: struct foo and function foo !?

        Prateek R Karandikar wrote:[color=blue]
        >[color=green][color=darkred]
        > > > Today I saw some code like this:
        > > >
        > > > struct foo {
        > > > foo(foo* s2){
        > > > cout << "foo::foo";
        > > > }
        > > > };
        > > >
        > > > foo* foo(foo *s2)
        > > > {
        > > > cout << "foo::(foo* )";
        > > > return s2;
        > > > }
        > > >
        > > > The name of the struct is 'foo' and the name of the
        > > > function is 'foo' also ! Why they are not conflicting ? The name of
        > > > types and the names of functions (and varialbles) are kept in separate
        > > > places ? Is this standard behaviour ?[/color][/color]
        >
        > Yes, unfotunately this is Standard behaviour. A struct and a function
        > in the same scope can have the same name. It is one of the unwanted
        > leftovers of the C language. In C, foo wouldn't be a type, struct foo
        > would be a type, so there was no ambiguity. In C++ now foo itself is a
        > type, but the committee chose not to break C compatibility in this
        > case. However it is best to avoid this "feature". A good compiler
        > should give a warning.
        >[/color]

        Just to add, a function declaration or definition will hide the name
        of a class /in the same scope/ (now you would need to say "struct foo"
        in order to get down to the name of the /struct/ foo).

        An interesting part is the function declarator itself:
        foo* foo(foo* s2)
        Here, foo* is still considered a pointer to struct foo since
        the declaration of function foo is not yet complete. If I were
        not able to change the names, I would use "struct foo" here too;
        otherwise, a forward declaration of function foo would cause a
        compilation error in the definition.

        Denis

        Comment

        • Razvan

          #5
          Re: struct foo and function foo !?

          kprateek88@yaho o.com (Prateek R Karandikar) wrote in message news:<607f883e. 0406271709.7c42 7a4a@posting.go ogle.com>...[color=blue][color=green][color=darkred]
          > > > Today I saw some code like this:
          > > >
          > > > struct foo {
          > > > foo(foo* s2){
          > > > cout << "foo::foo";
          > > > }
          > > > };
          > > >
          > > > foo* foo(foo *s2)
          > > > {
          > > > cout << "foo::(foo* )";
          > > > return s2;
          > > > }
          > > >
          > > > The name of the struct is 'foo' and the name of the
          > > > function is 'foo' also ! Why they are not conflicting ? The name of
          > > > types and the names of functions (and varialbles) are kept in separate
          > > > places ? Is this standard behaviour ?[/color][/color]
          >
          > Yes, unfotunately this is Standard behaviour. A struct and a function
          > in the same scope can have the same name. It is one of the unwanted
          > leftovers of the C language. In C, foo wouldn't be a type, struct foo
          > would be a type, so there was no ambiguity. In C++ now foo itself is a
          > type, but the committee chose not to break C compatibility in this
          > case. However it is best to avoid this "feature". A good compiler
          > should give a warning.[/color]

          Why are you saying that the function 'foo' is a type ? That
          makes no sense to me. What king of variables can you define with this
          type ?!!



          Regards,
          Razvan

          Comment

          • Richard Herring

            #6
            Re: struct foo and function foo !?

            In message <15f19d61.04062 80045.4783fcd0@ posting.google. com>, Razvan
            <mihai11@mailci ty.com> writes[color=blue]
            >kprateek88@yah oo.com (Prateek R Karandikar) wrote in message
            >news:<607f883e .0406271709.7c4 27a4a@posting.g oogle.com>...[color=green][color=darkred]
            >> > > Today I saw some code like this:
            >> > >
            >> > > struct foo {
            >> > > foo(foo* s2){
            >> > > cout << "foo::foo";
            >> > > }
            >> > > };
            >> > >
            >> > > foo* foo(foo *s2)
            >> > > {
            >> > > cout << "foo::(foo* )";
            >> > > return s2;
            >> > > }
            >> > >
            >> > > The name of the struct is 'foo' and the name of the
            >> > > function is 'foo' also ! Why they are not conflicting ? The name of
            >> > > types and the names of functions (and varialbles) are kept in separate
            >> > > places ? Is this standard behaviour ?[/color]
            >>
            >> Yes, unfotunately this is Standard behaviour. A struct and a function
            >> in the same scope can have the same name. It is one of the unwanted
            >> leftovers of the C language. In C, foo wouldn't be a type, struct foo
            >> would be a type, so there was no ambiguity. In C++ now foo itself is a
            >> type, but the committee chose not to break C compatibility in this
            >> case. However it is best to avoid this "feature". A good compiler
            >> should give a warning.[/color]
            >
            > Why are you saying that the function 'foo' is a type ? That
            >makes no sense to me. What king of variables can you define with this
            >type ?!!
            >[/color]
            Variables of type "pointer to {function taking one argument of type foo*
            and returning a foo*}".

            --
            Richard Herring

            Comment

            • John Harrison

              #7
              Re: struct foo and function foo !?


              "Razvan" <mihai11@mailci ty.com> wrote in message
              news:15f19d61.0 406280045.4783f cd0@posting.goo gle.com...[color=blue]
              > kprateek88@yaho o.com (Prateek R Karandikar) wrote in message[/color]
              news:<607f883e. 0406271709.7c42 7a4a@posting.go ogle.com>...[color=blue][color=green][color=darkred]
              > > > > Today I saw some code like this:
              > > > >
              > > > > struct foo {
              > > > > foo(foo* s2){
              > > > > cout << "foo::foo";
              > > > > }
              > > > > };
              > > > >
              > > > > foo* foo(foo *s2)
              > > > > {
              > > > > cout << "foo::(foo* )";
              > > > > return s2;
              > > > > }
              > > > >
              > > > > The name of the struct is 'foo' and the name of the
              > > > > function is 'foo' also ! Why they are not conflicting ? The name of
              > > > > types and the names of functions (and varialbles) are kept in[/color][/color][/color]
              separate[color=blue][color=green][color=darkred]
              > > > > places ? Is this standard behaviour ?[/color]
              > >
              > > Yes, unfotunately this is Standard behaviour. A struct and a function
              > > in the same scope can have the same name. It is one of the unwanted
              > > leftovers of the C language. In C, foo wouldn't be a type, struct foo
              > > would be a type, so there was no ambiguity. In C++ now foo itself is a
              > > type, but the committee chose not to break C compatibility in this
              > > case. However it is best to avoid this "feature". A good compiler
              > > should give a warning.[/color]
              >
              > Why are you saying that the function 'foo' is a type ? That
              > makes no sense to me. What king of variables can you define with this
              > type ?!!
              >[/color]

              function foo is not a type, struct foo is a type.

              john


              Comment

              • Razvan

                #8
                Re: struct foo and function foo !?

                Hi !




                int someFunc (int a)
                {
                return a + 2;
                }



                In order to declare a pointer to such a func you can use:

                typedef int (*ptr_to_func) (int);

                This is how you define a type of pointer to some function. Then you
                can write:


                ptr_to_func pFunc = someFunc;

                int rr = 10;
                cout << pFunc(rr) << endl;


                "someFunc" is not a type therefore you cannot use it like the type
                ptr_to_func is used.




                Regards,
                Razvan

                Comment

                Working...