Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Russell

C++ naming conflicts

This topic is 5247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What are the rules for when a name in C++ will conflict with other names? Today I did something, and I was suprised that it worked. Something like this, where there is a type and a member function with the same name.
#include <iostream>

enum Type { a, b, c };

class Class
{
public:
        Class () : i_(0) { }
        Class (int i) : i_(i) { }
        int Type () const { return i_; }
private:
        int i_;
};

int main ()
{
        using namespace std;

        Type t = a;

        Class c(t);

        cout << t << endl;
        cout << c.Type() << endl;

        return 0;
}
Where can I find out what the rules are for naming various things in a program?

Share this post


Link to post
Share on other sites
Advertisement
The rule is basically that a name can be used once per scope in C++. Your enum c is in the root namespace scope and the Class c is in the function local scope. Because the two names are in different scopes, they coexist. In this case, after the declaration of Class c in function scope, c will refer to the object unless you use the scope resolution operator ::.

Most C++ references will explain it. I don''t have a copy on hand but IIRC, Bjarne Stroustrup''s "The C++ Programming Language" has a good description.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!