-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNamespacesAndAliases.cpp
58 lines (44 loc) · 1.79 KB
/
NamespacesAndAliases.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Include our project header.
#include "NamespacesAndAliases.h"
// Include standard library headers.
#include <memory>
#include <functional>
#include <iostream>
/// Declare an alias `DataAggregateRef` for shared_ptr to that type.
/// The `Ref` suffix on a type name is a convention for share_ptr aliases.
using DataAggregateRef = std::shared_ptr<project::DataAggregate>;
/// Declare an alias to the module DataAggregate.
/// There is no ambiguity on the type, since it is in a different namespace from the previous.
using ModuleAggregate = project::module::DataAggregate;
/// Declare that we are using the std namespace implies the std:: prefix
/// on objects, variables, and methods inside the namespace.
/// We do this only in source (not header) files, since implying that we
/// are always using a given namespace can cause compiler ambiguity errors
/// and even unexpected runtime behavior if the compiler chooses incorrectly for you.
///
/// You can think of this as declaring an alias for every std:: object that drops the std::
using namespace std;
using namespace project;
///
/// Test program showing destructor behavior.
///
int main( int argc, char const *argv[] ) {
auto aggregate = DataAggregate( 1.0f, 2.0f );
auto agg2 = module::DataAggregate( 10.0f, "hello" );
return 0;
}
// Define our constructor for the DataAggregate in the project namespace.
project::DataAggregate::DataAggregate( float iOne, float iTwo )
: memberOne( iOne ),
memberTwo( iTwo )
{}
// We can also re-open a namespace in the source file and put our definitions in it
// in order to avoid the long chain of namespace::namespace:: specifications
namespace project {
namespace module {
DataAggregate::DataAggregate( float iValue, const string &iName )
: value( iValue ),
name( iName )
{}
} // namespace module
} // namespace project