Jump to content

Function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
Why does function ::f() return 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;

}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
My guess would be that int C is defined before class C, and since sizeof can accept either, it will take the first item defined. C::f(), on the other hand, will go with what it knows best, which is class C.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Nightracer

Nightracer

    Programmer

  • Members
  • PipPipPipPip
  • 131 posts

WingedPanther said:

My guess would be that int C is defined before class C, and since sizeof can accept either, it will take the first item defined. C::f(), on the other hand, will go with what it knows best, which is class C.

Agree

#4
The_Master

The_Master

    Newbie

  • Members
  • Pip
  • 4 posts
i also agree