473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Same name for a type and a method allowed?

I would like to have a method with a name same as some type in the
same namespace. Is it allowed by the C++ standard? I tried to figure
it out experimentally in gcc, but I get ambiguous results - for
example:

struct Point
{
int x;
int y;
};

struct Utils
{
Point Point()
{
return Point();
}

std::string Point()
{
return "";
}
};

The first method produces compile-time error, but the second method is
ok.
Mar 2 '08 #1
7 1232
On Mar 2, 9:36*am, mcz...@poczta.onet.pl wrote:
I would like to have a method with a name same as some type in the
same namespace. Is it allowed by the C++ standard? I tried to figure
it out experimentally in gcc, but I get ambiguous results - for
example:

struct Point
{
* * * * int x;
* * * * int y;

};

struct Utils
{
* * * * Point Point()
* * * * {
* * * * * * * * return Point();
* * * * }

* * * * std::string Point()
* * * * {
* * * * * * * * return "";
* * * * }

};

The first method produces compile-time error, but the second method is
ok.
I believe you need to provide the scope for Point() method.

Suresh M Shenoy
Mar 2 '08 #2
mc****@poczta.onet.pl wrote:
I would like to have a method with a name same as some type in the
same namespace. Is it allowed by the C++ standard? I tried to figure
it out experimentally in gcc, but I get ambiguous results - for
example:

struct Point
{
int x;
int y;
};

struct Utils
{
Point Point()
{
return Point();
}
Try to see if this works:

::Point Point()
{
return ::Point();
}
Mar 2 '08 #3
* Try to see if this works:
>
* * * * ::Point Point()
* * * * {
* * * * * * return ::Point();
* * * * }
Yes, this works with GCC. But is it compliant with ISO C++ for sure?
Mar 2 '08 #4
mc****@poczta.onet.pl wrote:
> Try to see if this works:

::Point Point()
{
return ::Point();
}

Yes, this works with GCC. But is it compliant with ISO C++ for sure?
It's perfectly legal, but it's still a poor choice of naming convention.

--
Ian Collins.
Mar 2 '08 #5
mc****@poczta.onet.pl wrote:
I would like to have a method with a name same as some type in the
same namespace. Is it allowed by the C++ standard?
Yes, but it's probably not a wonderful idea to exercise this particular
freedom. :)

struct point { };

void point() { }

int main() { point(); }
I tried to figure
it out experimentally in gcc, but I get ambiguous results - for
example:

struct Point
{
int x;
int y;
};

struct Utils
{
Point Point()
{
return Point();
}

std::string Point()
{
return "";
}
};
Huh? When client code calls Point(), which Point function is supposed
to be called?

If you need a function to create an object of a particular type (and
return it by value), a popular convention is to call the function
make_whatever. For example, see std::pair and std::make_pair.
Mar 2 '08 #6
It's perfectly legal, but it's still a poor choice of naming convention.

I have a special situation in a smart pointers library:

ref<Tsmart_ptr<T>::ref() const;

I think its simplicity is worth repeating the name.

Mar 3 '08 #7
mc****@poczta.onet.pl wrote:
>It's perfectly legal, but it's still a poor choice of naming convention.

I have a special situation in a smart pointers library:

ref<Tsmart_ptr<T>::ref() const;

I think its simplicity is worth repeating the name.
The standard library has at least two work-arounds for this situation:

(1) Give the type a _t extension, such that a method called size()
returns an object of type size_t (typedef'd to size_type).

(2) Abbreviate the name of the method, but not the name of the type.
For example, stringstream's str() method returns an object of type string.

A third option would be to define your ref<Ttemplate in a separate
namespace. The smart_ptr can provide a typedef for it, so that you will
have:

smart_ptr<T>::ref_type smart_ptr<T>::ref() const;
Mar 3 '08 #8

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

Similar topics

3
by: charlie fortune | last post by:
I have a simple html form and a bit of PHP in the same page, and I want the script to get the entry from the form, and write it to a file. I dont think I'm allowed to use POST on my webspace. Here...
2
by: piraticman | last post by:
Hi. I have some code for a javascript jump menu... It validated with HTML 4.0 but now does not validate with XHTML strict 1.0. I have used name atributes in my code which aparantly are not...
4
by: Martin Lucas-Smith | last post by:
I am wanting to know whether are XHTML1-valid characters for use within an id attribute and/or a name attribute. ...
1
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not...
4
by: Roger Withnell | last post by:
I want to validate the data entered into a form text field by calling a function with onblur, doing the validation and, if the input is invalid, giving an alert to the user and returning focus and...
5
by: Michelle Stone | last post by:
Hi everybody I am writing a simple asp.net application using form authentication. I store the list of all users and their passwords in an SQL Server database table. My client recently told me...
1
by: Ryan McLean | last post by:
Hi everyone! What is happening is the method: sub_btnSubmitClicked is being executed every time any other object with a Handler is executed. I am trying not to use the withevents and handles...
1
by: Andy | last post by:
Hi, I created a sample web service and tried calling it from my VBScript. Here it is: ---------------------------------------------------------------------------- --------------- '-- Declare...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...

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.