Skip to main content

Why does C++ allow a function and a class have a same name?

#include 

using namespace std;

struct A
{
    A()
    {
        cout << "A::A()" << endl;
    }
};

int A()
{
    cout << "void A()" << endl;
    return 0;
}

int main()
{
    auto v = A();
}

The output is:

void A()

Why does C++ allow a function and a class have a same name?

Solved

I believe this comes down to backwards compatibility with C.

In C, when you declare a struct like you did, you then have to refer to it as struct A, not just A. For example:

void A() {}

struct A {};

void f()
{
    A();

    struct A x; // works fine
    A y; // does not compile
}

In this context, it makes sense to allow A to mean two different things, because it's always clear which one you mean, depending on whether you used struct or not.

In C++, structs (and classes) can be referenced directly, without the need to use the struct keyword. This introduces the ambiguity you're concerned about, but the alternative is that valid C code like the one above would not be valid C++ code, which is even worse.


Why? Because that's the way the language is! The A::A() function belongs in a different "domain" than A().

It's similar to namespaces, where the same name can exist in multiple domains.

It's also somewhat similar to having a thousand different functions (or scopes) all having their own loop counter called i.


Comments

Popular posts from this blog

Can't find variable Page_ClientValidate in Safari

I have a ASP.NET 2.0 webpage with 3 server controls: RequiredFieldValidator , textbox , and button .` Button uses OnClientClick to call a JavaScript function for validation using Page_ClientValidate() . This page works fine with Chrome, IE, Firefox, Safari (5.1.7 on Windows). It has stopped working with modern Safari versions - 8.0 in Yosemite and Mobile Safari on iOS 8. I see this exception in the Inspect Element area: "ReferenceError: Can't find variable: Page_ClientValidate" How can this be fixed? Solved This sounds like a duplicate of this problem: iOS 8 / Safari 8 not working with ASP.NET AJAX-Extensions Another solution that modifies the web.config instead of the mozill: ReferenceError: Can't find variable: ValidatorEnable in Safari

Difficulty grasping Functional Dependency concepts with MySQL

I can write queries fairly well, but basic Database Design concepts continue to frustrate me :/ I'm working through an old book to try and brush up on the material, but functional dependencies are really making my head spin. For instance, the following question: Assume that Marcia keeps a table of data about her customers. Consider just the following part of that table: CUSTOMER(Phone, FirstName, LastName) Explain the conditions under which each of the following are true: Phone --> (FirstName, LastName) (Phone, FirstName) --> LastName (Phone, LastName) --> First Name (LastName, FirstName) --> Phone Phone --> --> LastName Phone --> --> FirstName Phone --> --> (FirstName, LastName) I don't even understand how to go about answering the question!.. Solved I'm not an expert on Db design, but from what I understand: And assuming that Phone is unique. True. Because a Client's's FirstName and Last Name can be uniquely de...