Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

multiple inheritance in C++

Name: Anonymous 2007-11-12 10:23

I always defended C++ but somehow it started to suck major ass.
This multiple inheritance stuff is killing me.
Example:

class a
{
public:
   a(int x){z = x}
   virtual void whatever() {}
private:
   int z;
};

class b
{
public:
   virtual void foobar() {}
};

class c : public virtual a, public virtual b
{
public:
  c(int x):a(x) {}
};

class d: public virtual c
{
public:
  d(int x):c(x) {}
};

class e: public virtual c
{
public:
  e(int x):c(x) {}
}

class f: public d, public e
{
public:
   ....
}

.
.
.


Holy shit!
I miss Scheme.

Name: Anonymous 2007-11-12 10:57

Looks like I got the constructors wrong.
Is that even solvable?

Name: Anonymous 2007-11-12 11:07

>>1
PROTIPS that may apply:
1. C++ sucks
2. Don't be a Cfag, much less a C++fag
3. Selective mixins > multiple inheritance
4. OOP is overrated, don't overdo it

Name: Anonymous 2007-11-12 11:09

>>2
Nope, It's NP-complete.

Name: Anonymous 2007-11-12 13:12

>>3
3. Why?

Name: Anonymous 2007-11-12 14:29

f(int x):d(x),e(x) {}

Name: Anonymous 2007-11-12 14:30

>>1
Don't blame the language, blame the programmer. Typically multiple inheritance is a sign of very poor design, and virtual inheritance is just fucking batshits insane. Anyway, the main problems are thus:

1) You never defined a virtual destructor in your base class. This leads to buttrape.

2) Too much virtual. ``c'' should not be using virtual inheritance - ``a'' and ``b'' don't share a common base class. The only classes that should be using virtual inheritance are ``d'' and ``e'' because they share a common base class, ``c''.

3) Constructor for ``f'' should explicitly call the appropriate contructors from ``d'' and ``e'', respectively.

Here's my version of your code, which compiles with GCC (though the actual behavior of the code is untested):

class a
{
        int z;
public:
   a(int x){z = x;}
   virtual void whatever() {}
   virtual ~a() {}
};

class b
{
public:
   virtual void foobar() {}
   virtual ~b() {}
};

class c : public a, public b
{
public:
  c(int x):a(x) {}
};

class d: public virtual c
{
public:
  d(int x):c(x) {}
};

class e: public virtual c
{
public:
  e(int x):c(x) {}
};

class f: public d, public e
{
public:
   f(int x) : e(x), d(x), c(x) {}
};

See http://www.parashift.com/c++-faq-lite/multiple-inheritance.html for more details.

Name: Anonymous 2010-11-27 17:40

<

Name: Anonymous 2010-12-06 9:06

Back to /b/, ``GNAA Faggot''

Don't change these.
Name: Email:
Entire Thread Thread List