Doubt regarding inheritance involving templates

D

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>'
with
[
T=fp_type
]
and
[
T=fp_type
]

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

Alf P. Steinbach

* 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.
 
M

Malte Starostik

darkstorm said:
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
 
M

Malte Starostik

Malte said:
^ 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
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top