473,787 Members | 2,928 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
7 2483
Tough question ?!!
Maybe somebody can pinpoint me to the right direction.

Razvan

mi*****@mailcit y.com (Razvan) wrote in message news:<15******* *************** *@posting.googl e.com>...
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

Jul 22 '05 #2
> > 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
-- --
Jul 22 '05 #3
Prateek R Karandikar wrote:
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
Jul 22 '05 #4
kp********@yaho o.com (Prateek R Karandikar) wrote in message news:<60******* *************** ****@posting.go ogle.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 ?!!

Regards,
Razvan
Jul 22 '05 #5
In message <15************ **************@ posting.google. com>, Razvan
<mi*****@mailci ty.com> writes
kp********@yah oo.com (Prateek R Karandikar) wrote in message
news:<60****** *************** *****@posting.g oogle.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
Jul 22 '05 #6

"Razvan" <mi*****@mailci ty.com> wrote in message
news:15******** *************** ***@posting.goo gle.com...
kp********@yaho o.com (Prateek R Karandikar) wrote in message

news:<60******* *************** ****@posting.go ogle.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
Jul 22 '05 #7
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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3982
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
4
5604
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can do a bsearch on the long member (nKey) but I am struggling to do a search using the string member. The code I am running appears below. It doesn't crash or anything. It is just that when I do the last bsearch using "192.168.1.3" I SHOULD find...
4
2116
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
19
2644
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; } RtWidget;
5
4356
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read that there is no difference between giving this function a
5
3332
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members of the function, but I have only the following horrific way to do that. Can someone please suggest a better alternative? #include <stdio.h> struct foo { char *a;
4
2593
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup } type; union { int (*func)(int); struct menu_item *submenu; } action;
4
5069
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
28
4717
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
160
5921
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.