473,325 Members | 2,671 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,325 software developers and data experts.

Scope visibility

Why doesn't the following work?

template <typename Type>
class A
{
public:
Type data_;
};

template <typename Type>
class B: public A<Type>
{
public:
void set()
{
data_ = 4;
}
};

int main()
{
B<int> b;
b.set();
return 0;
}

If you declare "using A<Type>::data_;" then it works. If you use normal
classes, this works without "using". I don't get it. Tested with
msvc2005 and comeau. Words from the standard?

--
Kalle Rutanen
http://kaba.hilvi.org
Mar 13 '06 #1
3 1730
On 13/3/06 22:43, in article MP************************@news.cc.tut.fi,
"Kaba" <no**@here.com> wrote:
Why doesn't the following work?

template <typename Type>
class A
{
public:
Type data_;
};

template <typename Type>
class B: public A<Type>
{
public:
void set()
{
data_ = 4;
}
};

int main()
{
B<int> b;
b.set();
return 0;
}

If you declare "using A<Type>::data_;" then it works.
But do you understand why that works?
If you use normal
classes, this works without "using". I don't get it. Tested with
msvc2005 and comeau. Words from the standard?


At the risk of doing your homework for you, have a read of

<http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18>

Apart from your example using a member variable and the example in the FAQ
using a member function, they're both remarkably similar! ;-)

--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

Mar 13 '06 #2
> > If you declare "using A<Type>::data_;" then it works.

But do you understand why that works?
If you use normal
classes, this works without "using". I don't get it. Tested with
msvc2005 and comeau. Words from the standard?


At the risk of doing your homework for you, have a read of

<http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18>

Apart from your example using a member variable and the example in the FAQ
using a member function, they're both remarkably similar! ;-)


Thank you Steve, this answered my question. No this is not homework, I
just found and switched off the "language extensions" option in visual
studio and got errors I had not seen before:)

--
Kalle Rutanen
http://kaba.hilvi.org
Mar 13 '06 #3
In article <MP************************@news.cc.tut.fi>,
Kaba <no**@here.com> wrote:
Why doesn't the following work?

template <typename Type>
class A
{
public:
Type data_;
};

template <typename Type>
class B: public A<Type>
{
public:
void set()
{
data_ = 4;
}
};

int main()
{
B<int> b;
b.set();
return 0;
}

If you declare "using A<Type>::data_;" then it works. If you use normal
classes, this works without "using". I don't get it. Tested with
msvc2005 and comeau. Words from the standard?


Have a look at
http://www.comeaucomputing.com/techt...membernotfound
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Mar 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Vipul Jain | last post by:
Can any one please tell me what is the difference between global scope of an variable and file scope of an variable. Vipul
3
by: richard pickworth | last post by:
Hello :) I am familiar with using scope resolution operator to define classes, but why does it turn up elsewhere? thanks richard
9
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. ...
12
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
6
by: Neelesh Bodas | last post by:
Hello All, I was just listing down various ways in which variables can be created and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3) Putting the summary here for corrections, comments,...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
1
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid,...
3
by: VRSki | last post by:
Hello, Is there any way to use "using" for aliasing in the global scope? The example below works fine in the context of a given file, but in the different file MyByte alias is unknown. Is...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.