C++ Crash Course: a fast-Paced Introduction


Namespaces and Implicit typedef of struct, union, and enum



Yüklə 7 Mb.
Pdf görüntüsü
səhifə30/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   26   27   28   29   30   31   32   33   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

Namespaces and Implicit typedef of struct, union, and enum
C++ treats type tags as implicit 
typedef
names. In C, when you want to use 

struct

union
, or 
enum
, you have to assign a name to the type you’ve created 
using the 
typedef
keyword. For example:
typedef struct Jabberwocks {
void* tulgey_wood;
int is_galumphing;
} Jabberwock;
In C++ land, you chortle at such code. Because the 
typedef
keyword can 
be implicit, C++ allows you instead to declare the 
Jabberwock
type like this:
struct Jabberwock {
void* tulgey_wood;
int is_galumphing;
};
This is more convenient and saves some typing. What happens if you also 
want to define a 
Jabberwock
function? Well, you shouldn’t, because reusing the 
same name for a data type and a function is likely to cause confusion. But if 
you’re really committed to it, C++ allows you to declare a 
namespace
to create 
different scopes for identifiers. This helps to keep user types and functions 
tidy, as shown in Listing 11.
#include
namespace Creature { 
u
struct Jabberwock {
void* tulgey_wood;
int is_galumphing;


xliv
An Overture to C Programmers
};
}
namespace Func { 
v
void Jabberwock() {
printf("Burble!");
}
}
Listing 11: Using namespaces to disambiguate functions and types with identical names
In this example, 
Jabberwock
the 
struct
and 
Jabberwock
the function now live 
together in frabjous harmony. By placing each element in its own 
namespace

the 
struct
in the 
Creature
namespace 
u
and the function in the 
Jabberwock
namespace 
v
–you can disambiguate which Jabberwock you mean. You can 
do such disambiguation in several ways. The simplest is to qualify the name 
with its 
namespace
, for example:
Creature::Jabberwock x;
Func::Jabberwock();
You can also employ a 
using
directive to import all the names in a 
namespace
, so you’d no longer need to use the fully qualified element name. 
Listing 12 uses the 
Creature
namespace
.
#include
namespace Creature {
struct Jabberwock {
void* tulgey_wood;
int is_galumphing;
};
}
namespace Func {
void Jabberwock() {
printf("Burble!");
}
}
using namespace Creature; 
u
int main() {
Jabberwock x; 
v
Func::Jabberwock();
}
Listing 12: Employing 
using namespace
 to refer to a type within the 
Creature
 namespace
The 
using namespace
u
enables you to omit the 
namespace
qualification 
v

But you still need a qualifier on 
Func::Jabberwock
, because it isn’t part of the 
Creature
namespace



An Overture to C Programmers
xlv
Use of a 
namespace
is idiomatic C++ and is a zero-overhead abstraction. 
Just like the rest of a type’s identifiers, the 
namespace
is erased by the com-
piler when emitting assembly code. In large projects, it’s incredibly helpful 
for separating code in different libraries.

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   26   27   28   29   30   31   32   33   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin