Re: "Linus Torvalds Was (Sorta) Wrong About C++"
Jorgen Grahn <grahn+nntp@snipabacken.se> writes:
On Sat, 2015-03-14, Stefan Ram wrote:
Supersedes: <inheritance-20150314185313@ram.dialup.fu-berlin.de>
[new quotation from Jorgen][modified diagram]
Jorgen Grahn <grahn+nntp@snipabacken.se> writes:
I don't see a reason for a ban on inheritance -- it's just that I
rarely find a good reason to use it.
Is anything wrong with:
ostream
^
|
.--------------'--------------.
| |
ostringstream ofstream
?
Nothing wrong, and I really like that I can output to a string
whenever I want. But I was just saying I /personally/, with the kind
of problems I solve, rarely (not never) find a reason to use
(= implement) inheritance.
I find that I use inheritence in one of two ways:
- To implement java-like "Interface" using pure abstract
base classes.
- To implement base-object semantics that can be applied to
multiple classes (e.g. a Thread class which is inherited by
any class which desires a pthread).
class c_thread: public c_dlist {
/**
* Thread identifier for this thread.
*/
pthread_t t_thread;
c_logger *t_logger;
std::string t_thread_name;
volatile bool t_running;
void insert(void);
static c_dlist t_thread_list;
static pthread_mutex_t t_lock;
static void lock(void) { pthread_mutex_lock(&t_lock); }
static void unlock(void) { pthread_mutex_unlock(&t_lock); }
static void *run(void *);
bool t_thread_initialized;
protected:
pthread_cond_t t_ready;
pthread_mutex_t t_threadlock;
void ignore_signals(void);
void lock_thread(void) { pthread_mutex_lock(&t_threadlock); }
void set_threadname(const char *);
void thread_initialized(void);
void unlock_thread(void) { pthread_mutex_unlock(&t_threadlock); }
public:
c_thread(const char *name, c_logger *);
virtual ~c_thread(void);
virtual void run(void) = 0;
virtual void terminate(void);
bool set_thread_affinity(cpu_set_t *, size_t);
bool is_running(void) { return t_running; }
bool in_context(void) { return t_thread == pthread_self(); }
void *join(void) { void *p; pthread_join(t_thread, &p);
return p; }
const char *get_thread_name(void) { return t_thread_name.c_str(); }
static void dump_threadlist(c_logger *);
static void initializer(void) __attribute__((constructor));
};