+7  A: 

No, but it can be faked to varying degrees with templates:

template<typename AbsIterator> 
class RichIterator : public AbsIterator {
public:
   template<typename FuncType>
   void foreach(FuncType f) { while( hasNext() ) f( next() ); }
};

class StringIterator {
  std::string m_s;
  int i;
public:
  typedef char T;
  StringIterator() : m_s(), i(0) {} // Unfortunately need this, or 
                                    // else RichIterator
                                    // gets way more complicated
  StringIterator(const std::string &s) : m_s(s), i(0) {}
  void swap(StringIterator& other) {
     m_s.swap(other.m_s);
     std::swap(i, other.i);
  }
  void reset_str(const std::string& s) {
     StringIterator(s).swap(*this);
  }
  bool hasNext() { return i < m_s.length(); }
  char next() { return m_s[i++]; }
};

template<typename Outputable>
void println(const Outputable& o) {
   std::cout << o << std::endl;
}

int main(int argc, char **argv) {
  typedef RichIterator<StringIterator> Iter;
  Iter iter;
  iter.reset_str(argv[1]);
  iter.foreach(&println<Iter::T>);
}

To be totally honest, I've haven't tested this by compiling it, but you should get the idea.

Logan Capaldo
+1  A: 

Some aspects of Scala mixins can be satisfied using multiple (virtual) inheritance. Unfortunately, this often introduces more problems than it solves. Also, you can't mix and match superclasses on the fly a la:

val me = new Human with Coder with Musician

If you really, really want true mixins, you almost have to go with something like the template solution proposed in the answer by @Logan Capaldo.

Daniel Spiewak
A: 

Hello,

Just to reiterate and extend what mentioned in previous emails let me first give you an example of how to implement the Scala Ordered trait in C++ and then let me show how you can mix-in an arbitrary number of "traits" at instantiation time.

Let's start first with the Ordered trait. If you are familiar with the Scala SDK you'll have noticed that there is an Ordered trait. This is used to provide total ordering by means of implementing a simple "compare" method. In C++, you could do the same as follows:

template <typename T>
class Ordered {
public:
    virtual int32_t compare(const T& that) = 0;
    bool operator >(const T& that) {return this->compare(that) == 1; }
    bool operator >=(const T& that) {return this->compare(that) >= 0; }

    bool operator ==(const T& that) { return this->compare(that) == 0; }

    bool operator <=(const T& that) {return this->compare(that) <= 0; }
    bool operator <(const T& that) {return this->compare(that) == -1; }
};

Then, to give ordering property to a C++ class you can do the following:

class MyOrderedType : public Ordered<MyOrderedType> {
public:
  // Your ctor/dtors, methods
public:
  int compare(const MyOrderedType& that);
};

Obviously you can mix-in as many "traits" as you want but if you do this way you can't add or remove traits at instantiation time. Is there a simple solution for this? Kind-of.

Have heard about the C++0x variadic templates? This provides a way of mixing-in as many traits as you want at template instantiation time.

The trick is simple, just declare your host class as follows:

template <typename... MIXINS>
class Host : public MIXINS... {
 // Your implementation
};

What is the catch here? Well, the issue is that it is not possible to do something like this:

template <typename... MIXINS>
class Host : public MIXINS<HOST>... {
    // Your implementation
};

Which in some cases would have been handy.

In any case, C++ has some basic mechanism that allow you to emulate some aspects of Scala's Mix-ins. What it can't do, however, is stacking behavior.

HTH.

acor