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

Doubt regarding inheritance involving templates

P: n/a
I have a doubt regarding inheritance involving templates

Consider this:
/////////////////////////////////////
template<typename T>
class A
{
private:
T m_a;

public:
virtual void Draw() = 0;
}
/////////////////////////////////////
template<typename T>
class B : public A<T>
{
public:
B(T w, T h);

private:
///data members....
}

template<typename T>
B<T>::B(T w, T h)
{
m_a = w * h;
}

Error:

error C2248: 'A<T>::m_a' : cannot access private member declared in class 'A<T>'
with
[
T=fp_type
]
and
[
T=fp_type
]

What is the reason behind it? Here I am using public inheritance....
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* darkstorm:
I have a doubt regarding inheritance involving templates

Consider this:
/////////////////////////////////////
template<typename T>
class A
{
private:
T m_a;

public:
virtual void Draw() = 0;
}
/////////////////////////////////////
template<typename T>
class B : public A<T>
{
public:
B(T w, T h);

private:
///data members....
}

template<typename T>
B<T>::B(T w, T h)
{
m_a = w * h;
}

Error:

error C2248: 'A<T>::m_a' : cannot access private member declared in class 'A<T>'


This error message has nothing to do with templates.

Private is private (almost always: you can override but not access).

The best way to fix this, assuming you really want private data in class A,
is to add a constructor for class A and an use an initializer list in the B
constructor. If, on the other hand, you want A's members to be accessible
to B, declare them protected instead of private. But that's not as good.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
darkstorm schrieb:
I have a doubt regarding inheritance involving templates

Consider this:
/////////////////////////////////////
template<typename T>
class A
{
private:
T m_a;

public:
virtual void Draw() = 0;
} ^ missing ; /////////////////////////////////////
template<typename T>
class B : public A<T>
{
public:
B(T w, T h);

private:
///data members....
} ^ missing ;
template<typename T>
B<T>::B(T w, T h)
{
m_a = w * h;
}

Error:

error C2248: 'A<T>::m_a' : cannot access private member declared in class 'A<T>'

What is the reason behind it? Here I am using public inheritance....


No different than without templates. m_a is private to class A, no
matter how B derives from it. To allow derived classes to access m_a,
make it protected. In most cases protected data members are a sign of
bad design though, A should provide a ctor that initialises m_a and an
accessor:

template< typename T >
class A
{
private:
T m_a;
protected:
A( const T& a ) : m_a( a ) {}
const T& a() const { return m_a; }
public:
virtual void Draw() = 0;
}

template< typename T >
class B : public A< T >
{
public:
B( const T& w, const T& h )
: A< T >( w * h ) {}

private:
///data members....
}

HTH,
Malte
Jul 23 '05 #3

P: n/a
Malte Starostik schrieb:
darkstorm schrieb:
template<typename T>
class A
{
private:
T m_a;

public:
virtual void Draw() = 0;
}


^ missing ;

template< typename T >
class A
{
private:
T m_a;
protected:
A( const T& a ) : m_a( a ) {}
const T& a() const { return m_a; }
public:
virtual void Draw() = 0;
}

^ well, and now the same error on my part *shakehead*

Cheers,
Malte
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.