9

I am just trying to check whether compiler allows type name as variable name. When i tried

int int;

It reported an error saying

error C2632: 'int' followed by 'int' is illegal

But when i tried

#include <string>
using namespace std;

int main()
{
    string string;
}

It didn't give any error. Both string and int are data types.

Why compiler allows string and doesn't allow int ?

EDIT: includes updated.

EDIT: Some people are saying that int is not a class. In that case, why below line is allowed.

int a(10);

it works similar to constructor of a class.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
bjskishore123
  • 6,144
  • 9
  • 44
  • 66

7 Answers7

22

string is not a C++ reserved word, but int is, and a reserved word cannot be used as an identifier.

And its syntactically fine to have class name and object name to be same.

class test {}; 
int main() { 
        test test; // test is an object of type test.
} 
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 14
    Note: Perfectly fine, but any maintainer is going to **kill** you if you do this in real code ;) +1 – Billy ONeal Aug 30 '10 at 14:40
  • 2
    This answer is correct, but I take issue with "its fine to have class name and object name to be same". The Standard may allow it, but it is very much not "fine." – John Dibling Aug 30 '10 at 14:42
  • 1
    It's even allowed to say `class test {}; test test;` in C++. – Johannes Schaub - litb Aug 30 '10 at 17:09
  • I disagree John. In most cases, yes, I would avoid it. But how about Color color? Stuff like that makes sense sometimes, nothing is absolute. – Ed S. Aug 31 '10 at 05:46
  • 1
    "class test {} test;" is also allowed – Markus Kull Aug 31 '10 at 06:38
  • 1
    Even worse: http://stackoverflow.com/questions/1995113/strangest-language-feature/2000491#2000491 – kennytm Aug 31 '10 at 09:15
  • @Ed: `Color color;` is fine, since the cases are different. There is no excuse for `Color Color;` though. If you _must_ use the same name as the class, just use static variables/constants, i.e., `Color::BLUE`. – Chinmay Kanchi Aug 31 '10 at 09:32
6

int is a C++ keyword. In the second declaration 'string string' declares an object of type 'std::string'. After this the name 'string' hides 'std::string' in an unqualified lookup

#include <string>
using std::string;

int main(){
    string string;

    string s;  // Error
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
3

int is a keyword, whereas string is the name of a class in the standard library but is not a keyword.

yfeldblum
  • 65,165
  • 12
  • 129
  • 169
1

string isn't actually a "data type" in the same sense the int is. int is a "native type" and as such, the text "int" is a keyword.

string is just a "user-defined" class (although, here the "user" the defined it is the C++ standards committtee). But as such, "string" is not a keyword.

So int int is two keywords together, but string string is just defining a varaible named "string" of type "string". The C++ compiler can keeps the separate two uses of "string" straight (but it's not a good idea to do this, since, as you've demonstrated, programmers often can't).

James Curran
  • 101,701
  • 37
  • 181
  • 258
0

Well, given that others have essentially posted the answer, I'm going to go ahead and post what I meant...

I'm assuming that because the second answer compiles, that you have using namespace std; in your file (which is in general not a good idea; I fail to see why people tell beginning C++ users to do this).

When the compiler goes to resolve the first string, it is able to find the class in namespace std. The second use of string is simply the name of the variable.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0

The compiler allows primitive types to behave like classes for the purposes of templates - if you had to specialize for primitives everywhere it would be a nightmare.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

Try removing

#include <string>
codaddict
  • 445,704
  • 82
  • 492
  • 529
user434966
  • 33
  • 4