Deriving and Template typedef

L

LuB

Not sure why this won't compile ... Is 'DataTypeT' not visible to Base?

I'd like to pass a data type to a base class ... with the data type
visible via typedef in a derived class.



#include <iostream>

template<typename T>
struct Base
{

Base(typename T::DataTypeT b)
{
std::cout << "DataValue: " << b << std::endl;
}

};

struct Derived : public Base<Derived>
{
typedef int DataTypeT;

Derived(int d) : Base<Derived>(d) { }

};

int
main(int argc, char** argv)
{
Derived d(5);
return 0;
}


Thanks,

-Luther
 
B

benben

LuB said:
Not sure why this won't compile ... Is 'DataTypeT' not visible to Base?

I'd like to pass a data type to a base class ... with the data type
visible via typedef in a derived class.



#include <iostream>

template<typename T>
struct Base
{

Base(typename T::DataTypeT b)
{
std::cout << "DataValue: " << b << std::endl;
}

};

struct Derived : public Base<Derived>
{
typedef int DataTypeT;

Derived(int d) : Base<Derived>(d) { }

The type, Derived, is not complete up to this point. You can use a
pointer to Derived, but not itself.
};

int
main(int argc, char** argv)
{
Derived d(5);
return 0;
}

But why over complicate the design? Go simple:

template <typename T>
struct Base
{
typedef T data_type;
Base(T b){/*...*/}
};


struct Derived: public Base<int>
{
Thanks,

-Luther

Regards,
Ben
 
L

LuB

benben said:
The type, Derived, is not complete up to this point. You can use a
pointer to Derived, but not itself.


But why over complicate the design? Go simple:

template <typename T>
struct Base
{
typedef T data_type;
Base(T b){/*...*/}
};


struct Derived: public Base<int>
{


Regards,
Ben

That is indeed what I have decided to do.

In this particular case, I am already passing 3 template parameters to
Base - so just trying to avoid another one and didn't understand why
that was breaking.

Many thanks for your input,

-Luther
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top