struct foo and function foo !?

R

Razvan

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
 
D

Denis Remezov

Prateek said:
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.

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
 
P

Prateek R Karandikar

Today I saw some code like this:
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
-- --
 
R

Razvan

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.

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
 
R

Richard Herring

Razvan said:
(e-mail address removed) (Prateek R Karandikar) wrote in message


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 ?!!
Variables of type "pointer to {function taking one argument of type foo*
and returning a foo*}".
 
J

John Harrison

Razvan said:
(e-mail address removed) (Prateek R Karandikar) wrote in message

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 ?!!

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

john
 
R

Razvan

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
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top