473,499 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typedef vs variable declaration inside a class

Consider the program

#include <iostream>

using namespace std;

class Test
{
public:
Test(Test_int c_value)
{
value = c_value;
}

typedef int Test_int;

private:
Test_int value;
};

int main()
{
Test t(100);

return 0;
}

Here I am getting compilation error for the line
Test(Test_int c_value)
because it uses Test_int type which is not known at that point and is
declared afterwards.

If I declare
typedef int Test_int;
before the the ctor, the compilation error goes. But the data member
'value' is used inside the ctor at which point the definition of the
variable is not known(it is defined later in the private section).

My doubt is why the compiler is unable to see the typedef declaration
for Type_int when it occurs later in the class; but it is able to see
the data member 'value' used in the ctor but which is actually defined
inside the private section later in the class.

Why are the typedef and variable definition inside the class treated
differently ?

Kindly explain

Thanks
V.Subramanian

Jun 26 '07 #1
4 7047
su**************@yahoo.com, India wrote:
Consider the program

#include <iostream>

using namespace std;

class Test
{
public:
Test(Test_int c_value)
{
value = c_value;
}

typedef int Test_int;

private:
Test_int value;
};

int main()
{
Test t(100);

return 0;
}

Here I am getting compilation error for the line
Test(Test_int c_value)
because it uses Test_int type which is not known at that point and is
declared afterwards.

If I declare
typedef int Test_int;
before the the ctor, the compilation error goes. But the data member
'value' is used inside the ctor at which point the definition of the
variable is not known(it is defined later in the private section).

My doubt is why the compiler is unable to see the typedef declaration
for Type_int when it occurs later in the class; but it is able to see
the data member 'value' used in the ctor but which is actually defined
inside the private section later in the class.

Why are the typedef and variable definition inside the class treated
differently ?

Kindly explain

Thanks
V.Subramanian
My understanding is that this rule exists to make life easier for
compiler writers. The C/C++ syntax is ambiguous (in the LALR(1) sense)
unless you know which names are typedefs and which names are just
ordinary identifiers. So special requirements are made of typedefs,
namely that you must declare them before you use them.

I read all this a long time ago, so don't take my word for it. Someone
else might have a better explanation.

john
Jun 26 '07 #2
Hi Subramanian,

Typedef is an alias for any valid type. It occupies the same namespace
as other identifiers and obeys the usual scope rules.

Applying scope rules once you declare typedef before ctor, you are
allowed to use typedef till the end of the class scope.
Hence it will work. But when you declare it after C'tor compile has
not still seen the typedef and hence does not know
the alias that you intend to use.

Hope this explains.
Ketan

On Jun 25, 9:49 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the program

#include <iostream>

using namespace std;

class Test
{
public:
Test(Test_int c_value)
{
value = c_value;
}

typedef int Test_int;

private:
Test_int value;

};

int main()
{
Test t(100);

return 0;

}

Here I am getting compilation error for the line
Test(Test_int c_value)
because it uses Test_int type which is not known at that point and is
declared afterwards.

If I declare
typedef int Test_int;
before the the ctor, the compilation error goes. But the data member
'value' is used inside the ctor at which point the definition of the
variable is not known(it is defined later in the private section).

My doubt is why the compiler is unable to see the typedef declaration
for Type_int when it occurs later in the class; but it is able to see
the data member 'value' used in the ctor but which is actually defined
inside the private section later in the class.

Why are the typedef and variable definition inside the class treated
differently ?

Kindly explain

Thanks
V.Subramanian

Jun 26 '07 #3
On Jun 26, 4:49 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the program
#include <iostream>
using namespace std;
class Test
{
public:
Test(Test_int c_value)
{
value = c_value;
}
typedef int Test_int;
private:
Test_int value;
};
int main()
{
Test t(100);

return 0;
}
Here I am getting compilation error for the line
Test(Test_int c_value)
because it uses Test_int type which is not known at that point and is
declared afterwards.
Correct.
If I declare
typedef int Test_int;
before the the ctor, the compilation error goes. But the data member
'value' is used inside the ctor at which point the definition of the
variable is not known(it is defined later in the private section).
Correct.
My doubt is why the compiler is unable to see the typedef declaration
for Type_int when it occurs later in the class; but it is able to see
the data member 'value' used in the ctor but which is actually defined
inside the private section later in the class.
Why are the typedef and variable definition inside the class treated
differently ?
They aren't, but the function body is treated differently from
the declaration. Informally, everything within the {...} of the
function is parsed and compiled as if it followed the class
definition. The part of the declaration which precedes the
opening brace isn't, however. (In the case of a constructor,
the initializer list is also processes as if it followed the
complete class definition.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 26 '07 #4
On Jun 26, 8:08 am, John Harrison <john_androni...@hotmail.comwrote:
subramanian10...@yahoo.com, India wrote:
Consider the program
#include <iostream>
using namespace std;
class Test
{
public:
Test(Test_int c_value)
{
value = c_value;
}
typedef int Test_int;
private:
Test_int value;
};
int main()
{
Test t(100);
return 0;
}
Here I am getting compilation error for the line
Test(Test_int c_value)
because it uses Test_int type which is not known at that point and is
declared afterwards.
If I declare
typedef int Test_int;
before the the ctor, the compilation error goes. But the data member
'value' is used inside the ctor at which point the definition of the
variable is not known(it is defined later in the private section).
My doubt is why the compiler is unable to see the typedef declaration
for Type_int when it occurs later in the class; but it is able to see
the data member 'value' used in the ctor but which is actually defined
inside the private section later in the class.
Why are the typedef and variable definition inside the class treated
differently ?
My understanding is that this rule exists to make life easier for
compiler writers. The C/C++ syntax is ambiguous (in the LALR(1) sense)
unless you know which names are typedefs and which names are just
ordinary identifiers. So special requirements are made of typedefs,
namely that you must declare them before you use them.
The rule you're thinking of applies to templates, and requires
the use of typename in some cases. Here, the rule has nothing
to do with whether the symbol is a type. Within function
bodies, default arguments, exception-specifications, and
constructor ctor-initializers (including such things in nested
classes), the class is considered complete; it's as if these
things were parsed after the closing brace of the class
definition. So using Test_int or value within the function
definition is legal, but using it before the opening brace of
the function (except as a default argument or in an exception
specifier) isn't.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
6415
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
2640
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
2
8794
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
2
4486
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
7
2129
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
12
2298
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
13
1826
by: miaohua1982 | last post by:
the code is as follows: #include <iostream> using namespace std; typedef double DOUBLE; class Screen { public: typedef int DOUBLE;
12
4608
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
4
2023
by: subramanian100in | last post by:
Consider the following: Class Test { public: Test(Test_int arg) : val(arg) { } typedef int Test_int; private:
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.