CB> There are a few parts of C++ that confuse me, and I was wondering if
CB> 1) the ? : thing
CB> an example I spotted in a message in here:
CB> regs.x.bx == 0xffff ? 2 : regs.x.bx
This is actually a C thing.
The expression
condition ? e1 : e2
evaluates to what the sub-expression e1 evaluates to if condition
evaluates to non-zero (true) and to what the sub-expression e2
evaluates to if condition evaluates to zero (false).
The function
int max(int i1, int i2)
{
return i1>i2 ? i1 : i2;
}
returns i1 if i1>i2 and i2 otherwise.
CB> 2) templates
Templates are used to parametrize functions and classes.
Making the above max function a template allows it to be used for
other types of parameters, provided that an appropriate operator> is
defined:
template
T max(T t1, T t2)
{
return t1>t2 ? t1 : t2;
}
void foo()
{
int i1 = 3,
i2 = 17;
int im = max(i1,i2);
// instantiates and calls int max(int,int)
double d1 = 3.1,
d2 = 4;
double dm = max(d1,d2);
// instantiates and calls double max(double,double)
}
There's a lot more to be written about templates. This is not the
appropriate place for that. Read a book.
CB> 3) deriving classes
If you write
class A
{
}
class B : public A
{
};
you define a class B which is (publicly) derived from class A.
There's a lot more to be written about class derivation and what it
allows: inheritance, polymorphism. This is not thhe appropriate place
for that. Read a book.
Thomas
---
þ MM 1.0 #0113 þ I can't remember if I used to know that.
---------------
* Origin: McMeier & Son BBS (2:301/138)
|