Author Topic: C++ : variable and class name identical?  (Read 14976 times)

Ken Fitlike

  • Jackass V
  • Posts: 1568
  • Karma: +25/-22
  • Ebeneezer McScrooge
C++ : variable and class name identical?
« on: September 01, 2005, 05:38:33 PM »
Why is this (ie. declaring a variable with an identical name as a class) allowed?
Code: [Select]
#include

class Widget
{
private:
  int t;
public:
  Widget():t(10){};
  int GetT()const {return t;}
};

int main()
{
Widget Widget; //<-- no problem declaring a variable with same name!?!

std::cout<}
I'd really appreciate an explanation of why this doesn't produce an error and whether it's something that is actually practiced much because it surely looks very strange to me.

Thanks in advance for your time and consideration.
And what rough beast, its hour come round at last,
Slouches towards Bethlehem to be born?.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
C++ : variable and class name identical?
« Reply #1 on: September 01, 2005, 06:03:45 PM »
Willing to bet its all about scoping rules.

Try this in your main and see the different results:
Add this to all examples:
Code: [Select]
std::cout<
Code: [Select]

Widget Widget; //<-- no problem declaring a variable with same name!?!
Widget bob;
 Ohh Widget isn't a type now

Code: [Select]

Widget bob;
Widget Widget; //<-- no problem declaring a variable with same name!?!

Works and compiles fine

Code: [Select]

Widget Widget; //<-- no problem declaring a variable with same name!?!
::Widget bob;

Ohhh you mean the global identifier named bob

Code: [Select]
int main()
{
        {
                Widget Widget; //<-- no problem declaring a variable with same name!?!
                std::cout<        }
        Widget bob;

        std::cout<
}

Ken Fitlike

  • Jackass V
  • Posts: 1568
  • Karma: +25/-22
  • Ebeneezer McScrooge
C++ : variable and class name identical?
« Reply #2 on: September 01, 2005, 06:47:27 PM »
Thanks a lot, Mike.

I had just assumed that since, for example, 'int int' was disallowed that the same would apply to user-defined types such as a class/struct. I'm still wondering if anyone would actually choose to do this sort of thing on a regular basis (other than maybe this guy, whose use of it initially prompted me to take a closer look)?
And what rough beast, its hour come round at last,
Slouches towards Bethlehem to be born?.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
C++ : variable and class name identical?
« Reply #3 on: September 01, 2005, 10:02:00 PM »
Personally if I saw a programmer do it I'd shoot them

ygfperson

  • Founders
  • Posts: 601
  • Karma: +10/-1
    • Last.fm
C++ : variable and class name identical?
« Reply #4 on: September 01, 2005, 10:27:04 PM »
I think objects and classes are probably completely separated from each other so it's allowed.

I know at least back in C you could do something like
Code: [Select]

struct int_pair {
  int a;
  int b;
  struct int_pair* next;
};
typedef struct int_pair int_pair;

This is useful if you wanted to have a struct int_pair that has a pointer to an object with the same type without specifying "struct" all the time. There's no way to do this kind of thing without an intermediate name for "struct int_pair" so you might as well name them the same since they mean exactly the same thing.

Curiously, C allows you to specify types like "struct ape_buggle* next;" without raising a fuss. It doesn't really care if these types don't exist if they're structs.

The closest I get to making the class look like the object is if there's an array thats a member of a class:
Code: [Select]

class Rutabega;
class Supermarket {
  Rutabega rutabegas[100];
};

When it's a member of a class and plural it can be forgiven imho since it can't pollute the namespace and it's descriptive.

Tman

  • Lok'tar!
  • Jackass I
  • Posts: 12
  • Karma: +10/-0
C++ : variable and class name identical?
« Reply #5 on: September 04, 2005, 01:09:45 AM »
Quote from: Mike
Personally if I saw a programmer do it I'd shoot them


Exactly. How hard is it to do

Code: [Select]
Widget widget;

I like to use ClassName for classes and var_name for anything else - keeps things clear.