C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

struct foo and function foo !?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Razvan
Guest





PostPosted: Sat Jun 26, 2004 4:28 pm    Post subject: struct foo and function foo !? Reply with 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
Razvan
Guest





PostPosted: Sun Jun 27, 2004 6:04 pm    Post subject: Re: struct foo and function foo !? Reply with quote



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





PostPosted: Sun Jun 27, 2004 10:12 pm    Post subject: Re: struct foo and function foo !? Reply with quote



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





PostPosted: Mon Jun 28, 2004 1:09 am    Post subject: Re: struct foo and function foo !? Reply with quote

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





PostPosted: Mon Jun 28, 2004 8:45 am    Post subject: Re: struct foo and function foo !? Reply with quote

[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





PostPosted: Mon Jun 28, 2004 12:37 pm    Post subject: Re: struct foo and function foo !? Reply with quote

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





PostPosted: Mon Jun 28, 2004 12:49 pm    Post subject: Re: struct foo and function foo !? Reply with quote


"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





PostPosted: Tue Jun 29, 2004 12:26 pm    Post subject: Re: struct foo and function foo !? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.