|
Boost : |
Subject: [boost] [BOOST CORE C++ Feature] The super or base keyword for C++
From: Max Hölzer (max.hoelzer_at_[hidden])
Date: 2011-04-29 17:48:51
Hello,
i was wondering if it makes sens to add the well known super or base keyword to
the C++ language in a generic way.
Here I have a simple example on how it would make inherited base or super class
overloaded function calls easier.
Example 1:
class A
{
public:
virtual void print() {
std::cout << "A" << std::endl;
}
};
class B : public A
{
public:
virtual void print()
{
std::cout << "B" << std::endl;
std::cout << "super call "; A::print();
}
};
class C : public B
{
public:
virtual void print()
{
std::cout << "C" << std::endl;
std::cout << "super call "; B::print();
}
};
int main(int argc, char* argv[])
{
A a;
B b;
C c;
a.print(); // Prints A
b.print(); // Prints B and super call A
c.print(); // Prints C and super call B super call A
return 0;
}
Adding a template class superable would avoid one to write the explicit base or
super class name.
Example 2:
namespace boost
{
template<typename T>
class superable : public T
{
public:
typedef T super; // C++ and java style
typedef T base; // C# style
};
}
class A
{
public:
virtual void print() {
std::cout << "A" << std::endl;
}
};
class B : boost::superable<A>
{
public:
virtual void print()
{
std::cout << "B" << std::endl;
std::cout << "super call "; super::print(); // C++ and java style
call
}
};
class C : boost::superable<B>
{
public:
virtual void print()
{
std::cout << "C" << std::endl;
std::cout << "super call "; base::print(); // C# style call
}
};
int main(int argc, char* argv[])
{
A a;
B b;
C c;
a.print(); // Prints A
b.print(); // Prints B and super call A
c.print(); // Prints C and super call B super call A
return 0;
}
If somebody could add this to the core boost library, that would be great.
Sincerely yours,
Max
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk