function ::f()

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

    function ::f()

    hi all:

    who can tell me why the function ::f() returns the size of the variable C
    not the size of type C?


    // details/inject.cpp

    #include <iostream>

    int C;

    class C {
    private:
    int i[2];
    public:
    static int f() {
    return sizeof(C);
    }
    };

    int f()
    {
    return sizeof(C);
    }

    int main()
    {
    std::cout << "C::f() = " <<C::f() << ","
    << " ::f() = " <<::f() << std::endl;
    }


  • Ivan Novick

    #2
    Re: function ::f()

    lizhuo wrote:
    hi all:
    >
    who can tell me why the function ::f() returns the size of the variable C
    not the size of type C?
    >
    #include <iostream>
    >
    int C;
    >
    class C {
    private:
    int i[2];
    public:
    static int f() {
    return sizeof(C);
    }
    };
    >
    int f()
    {
    return sizeof(C);
    }
    >
    int main()
    {
    std::cout << "C::f() = " <<C::f() << ","
    << " ::f() = " <<::f() << std::endl;
    }
    Check standard section [9.1]. "If a class name is declared in a scope
    where an object, function, or enumerator of the same name is also
    declared, then when both declarations are in scope, the class can be
    referred to only using an elaborated-type specifier"

    So in your case change the code like this:
    int f ()
    {
    return sizeof(class C);
    }

    -
    Ivan


    Comment

    • Salt_Peter

      #3
      Re: function ::f()


      lizhuo wrote:
      hi all:
      >
      who can tell me why the function ::f() returns the size of the variable C
      not the size of type C?
      Cause thats what you asked it to do.
      >
      >
      // details/inject.cpp
      >
      #include <iostream>
      >
      int C;
      Why not label and initialize the above non-const global appropriately:

      int g_n = 0;

      Consider sticking it into a namespace.
      >
      class C {
      private:
      int i[2];
      public:
      static int f() {
      return sizeof(C);
      }
      };
      >
      int f()
      {
      return sizeof(C);
      }
      >
      int main()
      {
      std::cout << "C::f() = " <<C::f() << ","
      << " ::f() = " <<::f() << std::endl;
      }
      And since you'll probably then do something like modify the global from
      within C::f() and f(), you'll probably write back asking why the
      variable does not change as ordained. And the answer to that will be:
      inject sequence points in your std::cout statements.

      Comment

      • Rolf Magnus

        #4
        Re: function ::f()

        Salt_Peter wrote:
        >
        lizhuo wrote:
        >hi all:
        >>
        >who can tell me why the function ::f() returns the size of the variable C
        >not the size of type C?
        >
        Cause thats what you asked it to do.
        >
        >>
        >>
        >// details/inject.cpp
        >>
        >#include <iostream>
        >>
        >int C;
        >
        Why not label and initialize the above non-const global appropriately:
        >
        int g_n = 0;
        You may think it's obvious why g_n is supposed to be a more appropriate name
        for that variable than C (besides it being different from the class name),
        but it's not.

        Comment

        • Salt_Peter

          #5
          Re: function ::f()


          Rolf Magnus wrote:
          Salt_Peter wrote:
          >

          lizhuo wrote:
          hi all:
          >
          who can tell me why the function ::f() returns the size of the variable C
          not the size of type C?
          Cause thats what you asked it to do.
          >
          >
          // details/inject.cpp
          >
          #include <iostream>
          >
          int C;
          Why not label and initialize the above non-const global appropriately:

          int g_n = 0;
          >
          You may think it's obvious why g_n is supposed to be a more appropriate name
          for that variable than C (besides it being different from the class name),
          but it's not.
          If what you are trying to say is the name doesn't really matter: i
          aggree: sizeof() is an operator, not a function. Ahem, took me 2 hours
          to figure that one out - shame.

          Comment

          • Greg

            #6
            Re: function ::f()


            Salt_Peter wrote:
            lizhuo wrote:
            >
            Why not label and initialize the above non-const global appropriately:
            >
            int g_n = 0;
            I don't see much benefit in assigning zero to a variable whose value is
            already zero.

            Greg

            Comment

            Working...