typedef int intarray[10]; intarray a, *p; p = &a;warning: illegal pointer combination
typedef int Int; Int i, *pi; pi = &i;
class A { public: A() { s = "XAX"; } void print() { cout << s; } private: char* s; }; class B : private A { }
A* a = new B;error
A* b = (A*) new B;accepted
b->print();XAX
char* p = (char*) b; while( *p != 'X' ) p++; cout << p << endl;XAX
int n = 7;value
int& r = n;reference to value n
int* p = &n;pointer to n
class sneaky {\fbox{sneaky}
private: int safe; public: sneaky() { safe = 12; } int& sorry() { return safe; } int value() { return safe; } };
sneaky x; cout << x.value() << endl; x.sorry() = 17; cout << x.value() << endl;Since a reference may occur on the left-hand side of an assignment, in contrast to a value, the data member safe may be assigned an arbitrary value, despite the fact that it occurs in the private section of the class sneaky. The remedy to this abuse would have been to declare that the member function sorry returns a
class A {\fbox{A}
public: A() { cout << "A"; } ~A() { cout << "A"; } }; class B : public A {\fbox{B}
public: B() { cout << "B"; } ~B() { cout << "B"; } };
The first example, given in slide 2-m-1,
defines two classes (A and B,
with B derived from A),
each having a constructor and destructor
writing the name of the class to standard output.
An example of their use is:
A* a = new B; delete a;
ABA
B* b = new B; delete b; ABBA
class A {\fbox{A}
public: A() { cout << "A"; } virtual ~A() { cout << "A"; } };