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

Inheritance and templates in g++

P: n/a
Hello,

I discovered a strange (?) template behaviour in g++.
It seems like I cannot access a variable from the baseclass
in a derived class without using a scope-operator or a cast.
This error doesn't occur with Borland C++. It also doesn't
occur when I don't use templates.

Is there a compiler-switch to change this behaviour?

template <typename T>
class CTest1
{
protected:

T a;
};

template <typename T>
class CTest2 : public CTest1<T>
{
public:

void Function1()
{
a = 0; // ERROR - a undeclared
}

void Function2()
{
CTest1<T>::a = 0; // WORKS
}

void Function3()
{
T(a) = 0; // WORKS
}
};

Best regards, Martin
Sep 27 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Martin Steen wrote:
Hello,

I discovered a strange (?) template behaviour in g++.
It seems like I cannot access a variable from the baseclass
in a derived class without using a scope-operator or a cast.
This error doesn't occur with Borland C++. It also doesn't
occur when I don't use templates.

Is there a compiler-switch to change this behaviour?

template <typename T>
class CTest1
{
protected:

T a;
};

template <typename T>
class CTest2 : public CTest1<T>
{
public:

void Function1()
{
a = 0; // ERROR - a undeclared
}

void Function2()
{
CTest1<T>::a = 0; // WORKS
}

void Function3()
{
T(a) = 0; // WORKS
}
};

Best regards, Martin
See the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-35.19

Cheers! --M

Sep 27 '06 #2

P: n/a
mlimber wrote:
>
See the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-35.19
Thank you, this is the answer I was looking for.
Seems like I have to live with that, although I still
think it's funny.

Best regards, Martin
Sep 27 '06 #3

P: n/a

mlimber wrote:
>
See the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-35.19
What if that is changed slightly:

template < typename T >
class B
{
public:
void f() {}
};

class A : public B<int{};

A a
a.f();

Does that work on all compilers?

Sep 27 '06 #4

P: n/a
Martin Steen <a0******************@spamgourmet.comwrites:
mlimber wrote:
See the FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-35.19

Thank you, this is the answer I was looking for.
Seems like I have to live with that, although I still
think it's funny.
It's sad, but it has to be that way because C++'s grammar is
screwed. The parser needs to know if a symbol denotes an object, a
type or a template in order to parse correctly, but this information
can't be obtained during parsing itself in these cases; so the
programmer has to provide this information manually.

Regards,

Jens
Sep 27 '06 #5

P: n/a
"Noah Roberts" <ro**********@gmail.comwrites:
What if that is changed slightly:

template < typename T >
class B
{
public:
void f() {}
};

class A : public B<int{};

A a
a.f();

Does that work on all compilers?
You changed two things:

1. made the member be a function rather than a type
2. made the access, a.f(), be not dependent on a template parameter
(because we're not in a template at all)

Each of those changes alone would be sufficient to work on any
standards compliant compiler.

Regards,

Jens
Sep 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.