views:

338

answers:

4

In all the languages that I understand this is not possible but someone was telling me it was possible in C++ but I have a hard time believing it. Essentially when you parameterize a class you are creating a unique class in the compilation stage aren't you?

Let me know if I am not being clear with my question.

Here is my attempt at explaning what I am trying to do ( pay attention to class L ):

//; g++ ModifingBaseClassParameter.cpp -o ModifingBaseClassParameter;ModifingBaseClassParameter

#include <iostream>

using namespace std;

template<typename T>
class Base
{
    public:
        Base() {}
        Base(T& t) : m_t(t) {}
        T& getMem() {return m_t;}
    private:
        T m_t;
};

template<typename T>
class F: Base<T>
{};

template<typename T>
class L: F<long>
{};

int main()
{
     Base<int> i;
     F<float> f;
     L<long> l;

     cout<<i.getMem()<<endl;
//     cout<<f.getMem()<<endl; // why doesn't this work
//     cout<<l.getMem()<<endl; // why doesn't this work
}

So as you can see (hopefully my syntax makes sense) class L is trying to redefine its parent's float parameter to be a long. It certainly doesn't seem like this is legal but I will differ to the experts.

A: 

Are you taking about templates?

template<typename T>
class Base
{
    public:
        Base() {}
        Base(T& t) : m_t(t) {}
        T& getMem() {return m_t;}
    private:
        T m_t;
};

class X: Base<int>
{};

class Y: Base<float>
{};
Martin York
Why not verify the question before launching into (uncompilable) code?
anon
Because I am addicted to writting.
Martin York
Martin I am not trying to be horrible to you or anyone else here on SO, but the best bet when anyone asks a vague question is always to wait for them to expand on it. If we all wait, think what a better site this would be! A big, happy balloon!
anon
I like big happy ballons. Though you are often terse, I personally have no problems with that, the fact that they are often correct makes up for it :-)
Martin York
+2  A: 

If you mean to ask whether you can do this in c++ :

template <> 
class ParamClass<Type1> : public ParamClass<Type2> 
{
};

then yes, it is possible.

It is very often used, for example to define template lists or inherit traits from another type.

Benoît
A: 

You should try compiling it:

$ g++ so-test1.c++ -o so-test1.c++ && ./so-test
so-test1.c++:21: error: expected template-name before ‘<’ token
so-test1.c++:21: error: expected `{' before ‘<’ token
so-test1.c++:21: error: expected unqualified-id before ‘<’ token
so-test1.c++: In function ‘int main(int, const char**)’:
so-test1.c++:27: error: aggregate ‘Z z’ has incomplete type and cannot be defined

X is not a class template, so it makes no sense to try to instantiate it in

class Z: X<long> {};

X has no template parameters to override. Remember that

Base<int>

is not a class template either; it is a class in its own right, but fully instantiated from a template. You could do this:

....
template<typename T>
class X: Base<T> 
{};
...
class Z: X<long> 
{};

But here there is no confusion about overriding any template parameters.

template<typename T>
class X: Base<int> 
{};
...
class Z: X<long>
{};

works too, but here the template parameter in X is unused, and nothing is overridden.

HTH

Tim Schaeffer
+1  A: 

What you've asked for can't be done directly -- but you can come pretty close by using a default template parameter:

template <typename T>
class Base { };

template <typename T = int>
class X : Base<T> {};

class Y : Base<float>

class Z : X<long> {};

In this particular case, the default template parameter doesn't add much. You have to supply a template parameter list to instantiate a template, even if defaults are provided for all the parameters. As such, having a default that you override in a derived class is usually only useful for the second and subsequent parameters.

Jerry Coffin