Scope visibility

K

Kaba

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?
 
S

Steve

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?"
 
K

Kaba

If you declare "using A said:
But do you understand why that works?


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:)
 
G

Greg Comeau

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/techtalk/templates/#whymembernotfound
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top