why this code has compile error

C

Chang PilHun

#include <iostream>
using namespace std;


template<class T>
class B1
{
public:
void SayHi()
{
T* pT = static_cast<T*>(this);
pT->PrintClassName();
}
protected:
void PrintClassName()
{
cout << "This is B1";
}
};


class D1:public B1<D1>
{
//No overridden function at all
};


class D2:public B1<D2>
{
protected:
void PrintClassName()
{
cout << "This is D2";
}
}
;

main()
{
D1 d1;
D2 d2;
d1.SayHi();
d2.SayHi();
}



--------compiler said
test.cpp: In member function `void B1<T>::SayHi() [with T = D2]':
test.cpp:43: instantiated from here
test.cpp:32: `void D2::printClassName()' is protected
test.cpp:12: within this context


why?
I never called this function.
.... Just.... c++'s philosophy?
 
J

John Harrison

Chang PilHun said:
#include <iostream>
using namespace std;


template<class T>
class B1
{
public:
void SayHi()
{
T* pT = static_cast<T*>(this);
pT->PrintClassName();
}
protected:
void PrintClassName()
{
cout << "This is B1";
}
};


class D1:public B1<D1>
{
//No overridden function at all
};


class D2:public B1<D2>
{
protected:
void PrintClassName()
{
cout << "This is D2";
}
}
;

main()
{
D1 d1;
D2 d2;
d1.SayHi();
d2.SayHi();
}



--------compiler said
test.cpp: In member function `void B1<T>::SayHi() [with T = D2]':
test.cpp:43: instantiated from here
test.cpp:32: `void D2::printClassName()' is protected
test.cpp:12: within this context


why?

Because its protected.
I never called this function.

Yes you did.

d2.SayHi();

calls

pT->PrintClassName();

with pT being of type D2*.
... Just.... c++'s philosophy?

It's the rules of C++, if that is what you mean. My suggestion would be to
make PrintClassName public, doesn't seem like it could do any harm.

john
 
C

Chad J McQuinn

Chang PilHun said:
template<class T>
class B1
{
public:
void SayHi()
{
T* pT = static_cast<T*>(this);
pT->PrintClassName();
}
protected:
void PrintClassName()
{
cout << "This is B1";
}
};
class D2:public B1<D2>
{
protected:
void PrintClassName()
{
cout << "This is D2";
}
}
;

main()
{
D1 d1;
D2 d2;
d1.SayHi();
d2.SayHi();
}
--------compiler said
test.cpp: In member function `void B1<T>::SayHi() [with T = D2]':
test.cpp:43: instantiated from here
test.cpp:32: `void D2::printClassName()' is protected
test.cpp:12: within this context
why?
I never called this function.

Yes, you did; not in main() but in SayHi(). It seems like you want to
use templates to implement virtual functions; you can do that to an
extent, but you can't take it quite this far. Let's remove the template
business and see what you are really doing:

void SayHi()
{
D2* d2 = static_cast<D2*>(this); // OK it your code
d2->PrintClassName(); // not allowed by access rules
}

This is an error because B1 does not have access to protected members
defined by its subclasses. You can call B1<T>::printClassName() at that
point in the code, but since it's not virtual, that won't do what you
want. So you will either need to use virtual functions, a friend
designation, or make the function public.

-Chad
 

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,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top