C++ inheritance question

Tyler Earman rem.intellegare@gmail.com
Fri Aug 21 14:26:00 GMT 2009


Hey guys,

I have a question regarding inheritance in C++ on GCC.  Now I've asked
this question in the past but I'd like to expound on it a little so we
can have a less hackish approach to this system.  I know GCC follows the
C++98 standard very well and its the standard that's at fault for this
little idiom, but I'd like to override it if possible.

Basically when a class inherits another class, specifically with
templates I believe, some of the methods within the first inherited
class become unaccessible without the "this->" operator (or a "using"
construct; the code is bellow).

Now I know the better way of working this is to use multiple inheritance
with virtual interfaces, but if possible I'd like to break the C++
standard right here for a moment, and enable GCC to compile the code
without complaining about this particular ABI.

Is there a way to do this?  I don't think -fpermissive works, but would
one of the older standards?

Thanks guys.

===== Code =======
#include <iostream>
#include <string>
#include <list>
using namespace std;
 
template <typename T>
class SelfOrganizingList: public list<T>
{
public:
  bool contains(const T& t)
  {
    typename list<T>::iterator it = find(this->begin(), this->end(), t);
    if (it == this->end()) return false;
    if (it != this->begin())
    { erase(it);
      push_front(t);
    }
    return true;  
  }
};
 
typedef SelfOrganizingList<string> List;  // defines List type
typedef List::iterator It;                // defines It type
 
void print(List& list)
{
  cout << "size = " << list.size();
  if (list.size() == 0) cout << ":\t()\n";
  else
  {
    It it = list.begin();
    cout << ":\t(" << *it++;
    while (it != list.end())
       cout << "," << *it++;
    cout << ")\n";
  }
}
 
int
main()
{
   cout << "List of computer languages using
SelfOrganizingList<string>...\n";
   List langs;
   langs.push_back("C");      print(langs);
   langs.push_back("Pascal"); print(langs);
   langs.push_back("Java");   print(langs);
   langs.push_back("C++");    print(langs);
   langs.push_back("Fortran");print(langs);
   cout << "\nHere is the sorted list:\n";
   langs.sort();              print(langs);
   if (langs.contains("Pascal"))
     cout << "Accessed Pascal in list, so it should move to the front:\n";
   print(langs);
   if (langs.contains("C++"))
     cout << "Accessed C++ in list so C++ should now move to the front:\n";
   print(langs);
}



More information about the Gcc-help mailing list