Re: Inheirting constructor?
On Nov 13 2010, 2:26 am, Ruslan Mullakhmetov <tiaba...@gmail.com>
wrote:
On 11/12/2010 9:02 PM, jbo5112 wrote:
You can probably accomplish what you want with templates. Here's an
example program.
#include<iostream>
enum children_classes { c1, c2 };
template< children_classes T>
class Base {
protected:
int a, b;
public:
Base(int a, int b) : a(a), b(b) {}
int foo();
};
template<>
int Base<c1>::foo()
{
// do something with a and b
return a + b;
}
template<>
int Base<c2>::foo()
{
// do something with a and b
return a * b;
}
typedef Base<c1> class1;
typedef Base<c2> class2;
int main() {
class1 obj1(2,3);
class2 obj2(2,3);
std::cout<< obj1.foo()<< std::endl;
std::cout<< obj2.foo()<< std::endl;
}
But as far as i understand that's not true inheritance. Rather you
defined interface Base<T> and implemented it in Base<c1>, Base<c2>. And
I can not figure out how are you going to use non-static polymorphism.
clarify please.
It's probably too late to be useful here anymore, but since nearly
everything is forever archived and indexed by Googl (not that I know
how they'd find it)e, here's my answer. No, it's not true
inheritance, but with careful construction and creative use of
includes I managed to simulate parent class, child classes and
grandchild classes with such templates. Personally, I avoid run-time
polymorphism for code that doesn't incur unnecessary overhead, and I
find templates intriguing. If you want to use non-static
polymorphism, you will need some actual inheritance. Here's the
solution, complete with default functions and tested with g++.
#include <iostream>
class Parent {
protected:
int a, b;
public:
Parent(int a, int b) : a(a), b(b) {}
virtual int foo()=0;
virtual int bar(int x, int y)=0;
};
enum children_classes { c1, c2, c3 };
template< children_classes T >
class Base : public Parent {
public:
Base(int a, int b) : Parent(a,b) {}
int foo() {return a/b;}
int bar(int x, int y) {return x/y;}
};
template<>
int Base<c1>::foo() {
// do something with a and b
return a + b;
}
template<>
int Base<c2>::foo() {
// do something with a and b
return a * b;
}
template<>
int Base<c1>::bar( int x, int y ) {
// do something with x and y
return x + y;
}
template<>
int Base<c2>::bar( int x, int y ) {
// do something with x and y
return x * y;
}
typedef Base<c1> class1;
typedef Base<c2> class2;
typedef Base<c3> class3;
int main() {
class1 obj1(4,2);
class2 obj2(4,2);
class3 obj3(4,2);
Parent *obj4;
char input;
std::cout << "Input 1 to point to obj1, or 2 to point to obj2\n";
while( std::cin.get(input).good() ) {
if( input>='1' && input<='3' ) {
if( input=='1' ) {
obj4=&obj1;
std::cout << "obj4 points to obj1\n";
}
else if( input=='2' ){
obj4=&obj2;
std::cout << "obj4 points to obj2\n";
}
else {
obj4=&obj3;
std::cout << "obj4 points to obj3\n";
}
std::cout
<< "obj1.foo()=" << obj1.foo()
<< "\tobj2.foo()=" << obj2.foo()
<< "\tobj3.foo()=" << obj3.foo()
<< "\nobj4.foo()=" << obj4->foo()
<< "\nobj1.bar()=" << obj1.bar(4,5)
<< "\tobj2.bar()=" << obj2.bar(4,5)
<< "\tobj3.bar()=" << obj3.bar(4,5)
<< "\nobj4.bar()=" << obj4->bar(4,5) << "\n\n";
}
}
}