File: embedding.yo

package info (click to toggle)
c%2B%2B-annotations 10.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,536 kB
  • ctags: 3,247
  • sloc: cpp: 19,157; makefile: 1,521; ansic: 165; sh: 128; perl: 90
file content (56 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download | duplicates (3)
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
Next, we embed the unrestricted union in a surrounding aggregate: tt(class
Data). The aggregate is provided with an tt(enum Tag), declared in its public
section, so tt(Data)'s users may request tags. tt(Union) itself is for
tt(Data)'s internal use only, so tt(Union) is declared in tt(Data)'s private
section. Using a tt(struct Data) rather than tt(class Data) we start out
in a public section, saving us from having to specify the initial tt(public:)
section for tt(enum Tag):
        verb(
    struct Data
    {
        enum Tag
        {
            INT,
            STRING
        };

        private:
            union Union
            {
                int         u_int;
                std::string u_string;

                ~Union();           // no actions
                // ... to do: declarations of members
            };

            Tag d_tag;
            Union d_union;
    };
        )

tt(Data)'s constructors receive tt(int) or tt(string) values. To pass these
values on to tt(d_union), we need tt(Union) constructors for the various union
fields; matching tt(Data) constructors also initialize tt(d_tag) to proper
values:
        verb(
    Data::Union::Union(int value)
    :
        u_int(value)
    {}
    Data::Union::Union(std::string const &str)
    :
        u_string(str)
    {}

    Data::Data(std::string const &str)
    :
        d_tag(STRING),
        d_union(str)
    {}
    Data::Data(int value)
    :
        d_tag(INT),
        d_union(value)
    {}
        )