Various C++ Examples (including IPC)  Version: 1.0.0
streambuf.cc
Go to the documentation of this file.
1 
13 #include <iostream>
14 #include <cstdio>
15 
17 class dmsg : public std::streambuf
18 {
19 public:
20  int i;
21  int *p;
22 public:
23 protected:
32  virtual int_type overflow(int_type c)
33  {
34  if(c != EOF)
35  {
37  if(putchar(c) == EOF)
38  return EOF;
39 
40  std::cout << "|";
41  }
42  return c;
43  }
44 };
45 
46 
47 int main(int argc, char **argv)
48 {
49  dmsg X;
50  std::ostream out(&X);
51 
52  out << "xxx "
53  << 123
54  << " yyy"
55  << std::endl;
56 
57  std::cout << "blah" << std::endl;
58 
59 
60  /* demonstrate compound input from a stream
61  (for example type "2xx4y [enter]" at the prompt)
62  */
63  int i;
64  char ch;
65  std::cout << std::endl;
66  std::cout << "i="<< i << "|ch="<<ch<<std::endl;
67  std::cout << "prompt:";
68  std::cin >> i >> ch;
69  std::cout << "i="<< i << "|ch="<<ch<<std::endl;
70 
71  /* demonstrate the contents of variables when they are created
72  */
73  dmsg *msg_p = new dmsg();
74 
75  if(msg_p->p == NULL)
76  std::cout << "msg_p->p is NULL" << std::endl;
77 
78  if(msg_p->i == 0)
79  std::cout << "msg_p->i is ZERO" << std::endl;
80 
81  delete msg_p;
82 
83 
84  int *i_p;
85 
86  if(i_p)
87  std::cout << "i_p is VALID" << std::endl;
88 
89  if(i_p == NULL)
90  std::cout << "i_p is NULL" << std::endl;
91  else
92  if(i_p == 0)
93  std::cout << "i_p is ZERO" << std::endl;
94 
95  i_p = new int;
96 
97  if(*i_p == 0)
98  std::cout << "*i_p is ZERO" << std::endl;
99  else
100  std::cout << "*i_p=" << *i_p <<std::endl;
101 
102  delete i_p;
103 }
int * p
Definition: streambuf.cc:21
virtual int_type overflow(int_type c)
Definition: streambuf.cc:32
int i
Definition: streambuf.cc:20
Demonstration of streambuf inheritance.
Definition: streambuf.cc:17
int main(int argc, char **argv)
Definition: streambuf.cc:47