473,387 Members | 1,542 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.

Doubt regarding inheritance involving templates

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

Similar topics

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...
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...
3
by: cspirit | last post by:
Hi all, While reading some propriotary C++ code, I came across following constructor: ClassA::ClassA(arg1,arg2) : ClassB(arg_1, arg_2) >From my limited knowledge of C++, i can guess that this...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
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: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
6
by: thorsten.schilling | last post by:
Hello everybody, the question is, what is the "golden way" or the best way, if I have a memberfunction in a class, which should return a new instance of an object. For example some class Foo...
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: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). --...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.