C
Chetan Raj
Hi All,
I don't understand why the following code does not compile...
#include <iostream>
using namespace std;
class A
{
protected:
A()
{
cout<<" I am A;";
x = 5;
}
int GiveX()
{
return x;
}
int x ;
};
class B : public A
{
public:
B()
{
A* p = new A();
cout<<"I am B with x = "<<GiveX()<<endl;
}
};
int main(int argc, char* argv[])
{
B b;
return 0;
}
The compiler will show the following error at line containg A* p =
new A();'A::A' : cannot access protected member declared in class 'A'
<<
but not when I use only GiveX() function.
class B derives publicly form A. Both A() and GiveX() are protected
functions in class A.
When we can use the function GiveX(), why cannot we use the protected
constructor?
Can anyone explain this behaviour?
Thanks in advance,
Chetan Raj
I don't understand why the following code does not compile...
#include <iostream>
using namespace std;
class A
{
protected:
A()
{
cout<<" I am A;";
x = 5;
}
int GiveX()
{
return x;
}
int x ;
};
class B : public A
{
public:
B()
{
A* p = new A();
cout<<"I am B with x = "<<GiveX()<<endl;
}
};
int main(int argc, char* argv[])
{
B b;
return 0;
}
The compiler will show the following error at line containg A* p =
new A();'A::A' : cannot access protected member declared in class 'A'
<<
but not when I use only GiveX() function.
class B derives publicly form A. Both A() and GiveX() are protected
functions in class A.
When we can use the function GiveX(), why cannot we use the protected
constructor?
Can anyone explain this behaviour?
Thanks in advance,
Chetan Raj