function ::f()

L

lizhuo

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;
}
 
I

Ivan Novick

lizhuo said:
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
http://www.0x4849.net
 
S

Salt_Peter

lizhuo said:
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.
 
R

Rolf Magnus

Salt_Peter said:
Cause thats what you asked it to do.


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

Salt_Peter

Rolf said:
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.
 
G

Greg

Salt_Peter said:
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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top