Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Experts,

I am inheriting class B from class A in protected mode. But I am unable to assign the derived class object to the base class pointer as

MIDL
A *ptr;
    B b(30,50);
    ptr=&b;


The same issue is happened in private mode inheritance.But if we inherit it in public mode then everything is ok. What I am missing.? Any idea.? Please help me on this.

My code


XML
#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <cassert> // for assert()
using namespace std;

class A
{
public:
int a;
    A(int t)
    {
        a=t;
    }
     void Fun_A()
    {
    cout<<"Class A Fun()"<<a;
    }

};

class B: protected A // Private mode has the same problem But in public mode it is ok
{
    public:
    int b;
    B(int t,int x):b(t),A(x)
    {

    }

};

void main(void)
{
    A *ptr;
    B b(30,50);
    ptr=&b;

}




Thanks
Arun
Posted
Updated 24-Apr-11 21:38pm

The reason for this error is because the base class should not be accessible by whomever instantiates the object when derived as protected or private. This behavior can be overriden by typecasting as others have suggested, but it is not recommended and is expected behavior.

See a good explanation here:
http://vapvarun.com/study/softE/john%20wiley%20and%20sons%20-%20programming%20with%20object-oriented%20programming/5399final/lib0164.html[^]

If you want to access the base pointer, then you want a public derivation.
 
Share this answer
 
v2
Comments
Nish Nishant 25-Apr-11 12:20pm    
Good answer, voted 5.
Albert Holguin 25-Apr-11 12:57pm    
thanks :)
Arun India 25-Apr-11 23:41pm    
Thanks Albert Holguin, This is the answer which I expect. My 5
Albert Holguin 26-Apr-11 0:02am    
thanks and happy coding :)
I think you are getting error C2243.
Since you are inherited A in protected mode you can not access the Class A part from Class B.Check the links for more info.

Msdn(Error info):
http://msdn.microsoft.com/en-us/library/s5480d1f.aspx[^]

Stackoverflow:
http://stackoverflow.com/questions/1471800/getting-rid-of-error-c2243[^]
 
Share this answer
 
Comments
Arun India 25-Apr-11 4:00am    
But why it is happening.? I can create the object of the derived class. No problem. But when I assign it to the base pointer it making problem. Strange..?
Albert Holguin 25-Apr-11 11:26am    
your statement "you can not access the Class A part from Class B" is incorrect
Error no error C2243 comes for lots of Typecasting issues
You may try like follows
A *ptr;
B *b(30,50);
ptr=(A*)b;
 
Share this answer
 
Comments
Arun India 25-Apr-11 6:55am    
Yes Santhosh, if I use the following code it is working fine.
A *ptr;
B b(30,50);
ptr=(A*)&b;

But still my doubt is there. Why I am not able to assign the address of the class B to base class pointer like
A *ptr;
B b(30,50);
ptr=&b;

Could you explain..?
Himansu sekhar bal 25-Apr-11 7:52am    
The problem is that because of protected inheritance, the default constructor is not accessible
Albert Holguin 25-Apr-11 11:25am    
see my solution
The problem is that because of protected inheritance, the default constructor is not accessible. Although, I don't understand why this is be an issue considering that protected members are supposed to be accessible by derived classes. I'll admit, however, I'm not too familar with inheritance other than Public inheritance because I've never needed to use any kind of inheritance other than Public. Why do you want your class to have protected inheritance?
 
Share this answer
 
You need an explicit cast opertor:
class B: protected A // Private mode has the same problem But in public mode it is ok
{
public:
  A*  operator & (){ return this; }
  // other members
};

and
MIDL
A *ptr;
B b(30,50);
ptr=&b;

will work as expected.
Regards.
 
Share this answer
 
Comments
Albert Holguin 25-Apr-11 11:24am    
this is true but makes little sense to derive protected or private simply to override the protection with a typecast
mbue 25-Apr-11 12:57pm    
Its not my decision.
Usually virtual classes should be full proteced except the virtual members. Thats the background of COM or interfaces generally too. The pure virtual members are public and everyting else is protected. So the user can only call the virtual members.
Regards.
Albert Holguin 25-Apr-11 13:00pm    
this is true, but you should probably inform the OP of what they're overriding... a good habit I've picked up from SA...
mbue 25-Apr-11 13:58pm    
I think the op has to learn more about the fundamentals of the c++ concept, before asking such questions. therefore i'm with you ;-)
Regards.
Hi,

If you F1 on the error itself you will get the explanation that the conversion is possible but is inaccessible.

Access protection prevented the conversion from happening, hence it is possible in public & not in private or protected.
 
Share this answer
 
Comments
Himansu sekhar bal 25-Apr-11 8:01am    
u r correct
Arun India 25-Apr-11 8:25am    
That means, we can conclude that- if we are using base pointer to derived objects, then we should publicly inherit the base class to derived class(Not in private or protected mode). Correct..?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900