473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inheritance and templates in g++

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
5 1809
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
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

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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
5
by: Dart | last post by:
What are the major advantages of inheritance of a class of no virtual functions, promoting code re-use? Thanks!
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
6
by: Ravi Rao | last post by:
Hi, This is about "templates vs inheritance" (please, before you flame me, I do understand that they cover largely non-intersecting domains). Apart from D&E*, I've found either online...
4
by: Joe | last post by:
We are working on a project that requires 3rd parties (ie, consultants) to use our ASP.net webforms in a very reusable manner. The big catch is that we are not allowed to give them source. There...
4
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
2
by: Isaac Gelado | last post by:
Hi, I am having problems with inheritance in templates classes. Say I have the following classes: class A {}; class B: public A {}; template<typename Tclass C {}; Now in my code I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.