 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Razvan Guest
|
Posted: Sat Jun 26, 2004 4:28 pm Post subject: 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
|
|
| Back to top |
|
 |
Razvan Guest
|
Posted: Sun Jun 27, 2004 6:04 pm Post subject: Re: struct foo and function foo !? |
|
|
Tough question ?!!
Maybe somebody can pinpoint me to the right direction.
Razvan
[email]mihai11 (AT) mailcity (DOT) com[/email] (Razvan) wrote in message news:<15f19d61.0406260828.7cc55 (AT) posting (DOT) google.com>...
| Quote: | 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
|
|
|
| Back to top |
|
 |
Denis Remezov Guest
|
Posted: Sun Jun 27, 2004 10:12 pm Post subject: Re: struct foo and function foo !? |
|
|
Prateek R Karandikar wrote:
| Quote: |
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 ?
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
|
|
| Back to top |
|
 |
Prateek R Karandikar Guest
|
Posted: Mon Jun 28, 2004 1:09 am Post subject: Re: struct foo and function foo !? |
|
|
| Quote: | 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 ?
|
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
-- --
|
|
| Back to top |
|
 |
Razvan Guest
|
Posted: Mon Jun 28, 2004 8:45 am Post subject: Re: struct foo and function foo !? |
|
|
[email]kprateek88 (AT) yahoo (DOT) com[/email] (Prateek R Karandikar) wrote in message news:<607f883e.0406271709.7c427a4a (AT) posting (DOT) google.com>...
| Quote: | 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 ?
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
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Mon Jun 28, 2004 12:37 pm Post subject: Re: struct foo and function foo !? |
|
|
In message <15f19d61.0406280045.4783fcd0 (AT) posting (DOT) google.com>, Razvan
<mihai11 (AT) mailcity (DOT) com> writes
| Quote: | kprateek88 (AT) yahoo (DOT) com (Prateek R Karandikar) wrote in message
news:<607f883e.0406271709.7c427a4a (AT) posting (DOT) google.com>...
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 ?
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 ?!!
Variables of type "pointer to {function taking one argument of type foo* |
and returning a foo*}".
--
Richard Herring
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Mon Jun 28, 2004 12:49 pm Post subject: Re: struct foo and function foo !? |
|
|
"Razvan" <mihai11 (AT) mailcity (DOT) com> wrote
| Quote: | kprateek88 (AT) yahoo (DOT) com (Prateek R Karandikar) wrote in message
news:<607f883e.0406271709.7c427a4a (AT) posting (DOT) google.com>...
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 ?
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 ?!!
|
function foo is not a type, struct foo is a type.
john
|
|
| Back to top |
|
 |
Razvan Guest
|
Posted: Tue Jun 29, 2004 12:26 pm Post subject: 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|