By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

Deriving and Template typedef

P: n/a
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

Apr 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
LuB wrote:
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>
{
Derived(int d): Base<int>(d){}
};

Thanks,

-Luther


Regards,
Ben
Apr 21 '06 #2

P: n/a
LuB

benben wrote:
LuB wrote:
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>
{
Derived(int d): Base<int>(d){}
};

Thanks,

-Luther


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

Apr 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.