typedef problem

R

rajm2019

hi to all

i am new to c++ i want to know wat do we mean bye

typedef support::ptrObject<Directory> DirectoryRef

when we are using (DirectoryRef abc);

is abc an object of template class support::ptrObject<Directory> or
something else.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

hi to all

i am new to c++ i want to know wat do we mean bye

typedef support::ptrObject<Directory> DirectoryRef

when we are using (DirectoryRef abc);

is abc an object of template class support::ptrObject<Directory> or
something else.

Yes, typedef is used to create an alias for a type. If you understand
macros it's much the same as

#define DirectoryRef support::ptrObject<Directory>

meaning that everywhere you type DirectoryRef it will be replaced with
support::ptrObject<Directory>.
 
I

Ian Collins

hi to all

i am new to c++ i want to know wat do we mean bye

typedef support::ptrObject<Directory> DirectoryRef

when we are using (DirectoryRef abc);

is abc an object of template class support::ptrObject<Directory> or
something else.
Yes, a typedef is an alias for an existing type, not a new type.
 
N

Neelesh Bodas

Yes, typedef is used to create an alias for a type. If you understand
macros it's much the same as

#define DirectoryRef support::ptrObject<Directory>

meaning that everywhere you type DirectoryRef it will be replaced with
support::ptrObject<Directory>.

If I understand right, the types are *not* "replaced" when typedef is
used. also, typedef is fundamentally a different construct than macro
replacement. Here is an example:

typedef int* foo; // foo is a typedef for int*
#define foo2 int* // foo2 is a macro for int*

foo a, b // same as int* a, int* b
foo2 a, b // same as int* a, int b

Quoting from 7.1.3(1):
"Within the scope of its declaration, a typedef name is syntactically
equivalent to a keyword and names the type associated with the
identifier.... A typedef name is thus a synonym for another type."

Hope this clears the difference between typedef and #define.

~Neelesh
 
J

James Kanze

If I understand right, the types are *not* "replaced" when typedef is
used.

It depends on what you mean by "replaced", but typedef does not
introduce a new type. In the example, when you use
DirectoryRef, it's exactly as if you'd used
support::PtrObject said:
also, typedef is fundamentally a different construct than macro
replacement.

Replacement occurs at a fundamentally different phase of
compilation. And of course, unlike macros, typedef's obey all
of the usual scope and hiding rules. (I'd like to see you
replace the typedef of value_type in the standard containers
with a #define.)
Here is an example:
typedef int* foo; // foo is a typedef for int*
#define foo2 int* // foo2 is a macro for int*
foo a, b // same as int* a, int* b
foo2 a, b // same as int* a, int b

You don't even need to go so far:

const foo a ; // same as int* const
const foo2 b ; // same as int const*

(This is, BTW, the strongest argument I know for always putting
the const after what it modifies. Unless, of course, it is an
argument against typedef'ing derived types.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top