3

One Book(Object Oriented Programming with C++ by E.Balagurusamy) says that

 const size = 10;  

means

 const int size = 10; 

but g++ compiler (version-4.6.1 in ubuntu) issues an error as

error: ‘size’ does not name a type 

what should I conclude based on this?

  1. g++ doesn't support the feature.
  2. It is new feature. Latest g++ version supports it.
  3. The statement is wrong. Data-type is mandatory with the const keyword.
  4. Something else.
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
Mohammed H
  • 6,880
  • 16
  • 81
  • 127

3 Answers3

5

Mr. Balagurusamy is wrong. Completely wrong. The type is compulsory, with or without const keyword.

You should stop reading this book. You should not read books by Yashwant Kanetkar also. I know that books by these authors are very popular among students of many universities in India.

I would suggest you to get an introductory book from this list:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Oh my god! The C++ book list doesn't list E.Balagurusamy's book. We consider his books are as Bible for Programming Language. – Mohammed H Mar 29 '12 at 17:01
  • 2
    @habeebperwad If Balagurusamy's C++ book contains a mistake such as claiming that 'const size' means 'const int size' then you should seriously reconsider your opinion of his book. – bames53 Mar 29 '12 at 17:50
3

Looks like an error in the book ... you definitely must name a type or aliased type (i.e., a typedef) since C++ is a strongly-typed languages.

Here is what the C++03 specification states on objects, declarations and definitions:


Section 1.8/1:

The properties of an object are determined when the object is created. An object can have a name (clause 3). An object has a storage duration (3.7) which influences its lifetime (3.8). An object has a type (3.9). The term object type refers to the type with which the object is created.

Then in Section 3.1/1:

A declaration is a definition ... [note: the rest of the paragraph are exceptions to this rule and are omitted since they're not applicable in this case]

Then in Section 3.1/6:

A program is ill-formed if the definition of any object gives the object an incomplete type

Finally, in Section 3.9.2/1 it states:

... the term object type (1.8) includes the cv-qualifiers specified when the object is created. The presence of a const specifier in a decl-specifier-seq declares an object of const-qualified object type; such object is called a const object. ...


So according to 3.9.2/1, const is a qualifier, not a type, and as-such, it has to qualify a valid unqualified type. Secondly, in the example given, according to 3.1/1, the declaration of size is also a definition, and therefore the size object must have an associated type or the program is ill-formed according to 3.1/6.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • Not error. It says so. And in some program examples, they used such statements. – Mohammed H Mar 29 '12 at 16:48
  • I see I've been downvoted ... any reasons? If there is a mistake, I think it helps everyone to know what that mistake is and get it fixed, rather than simply do a drive-by downvote. – Jason Mar 29 '12 at 18:14
  • I wasn't the downvoter, but C is not a subset of C++ (although it comes close), and certainly there are features in C99 that don't exist until C++11 which is not in widespread use yet. – Mark B Mar 29 '12 at 18:21
3

I would go with: The statement is wrong. Data-type is mandatory with the const keyword.

Darren
  • 68,902
  • 24
  • 138
  • 144