473,473 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VC++ Inheritance and protected members

Hi,

I have a couple of classes that are causing compile errors. I hope someone
can shed some light as to why.

class A
{
public:
A(X* pX) : m_pX(pX) {};

protected:
X* m_pX;
};

class SubA : public A
{
public:
SubA(A& a) : A(a.m_pX) {};
};

When I compile this, I get an error stating that I can't access protected
member A::m_pX.

Would someone be so kind as to 'splain this to me?

Thanks!

JAB

--
--------------------------------------------------------
John A. Byerly
Engineered Software Solutions, Ltd.
http://www.ess-quality.com
Jul 22 '05 #1
7 1951
John A. Byerly wrote:
I have a couple of classes that are causing compile errors. I hope someone
can shed some light as to why.

class A
{
public:
A(X* pX) : m_pX(pX) {};

protected:
X* m_pX;
};

class SubA : public A
{
public:
SubA(A& a) : A(a.m_pX) {};
};

When I compile this, I get an error stating that I can't access protected
member A::m_pX.

Would someone be so kind as to 'splain this to me?


You can't access protected members of a class different from the one from
which you try to access the protected members. You can rewrite your code
as

SubA(SubA& a) : A(a.m_pX) {}

_or_ give class A an accessor function that would return m_pX:

class A
{
...
X* get_pX() { return m_pX; }
...
};

SubA(A& a) : A(a.get_pX()) {}

And this problem has nothing to do with VC++ since it's the Standard C++
behaviour, often misunderstood.

Also, if you need to ask a VC++ question (which this one wasn't), you will
find microsoft.public.vc.language helpful. This particular question is
not compiler-specific, however.

V
Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4y*******************@newsread1.mlpsca01.us.t o.verio.net...
John A. Byerly wrote:
I have a couple of classes that are causing compile errors. I hope someone can shed some light as to why.

class A
{
public:
A(X* pX) : m_pX(pX) {};

protected:
X* m_pX;
};

class SubA : public A
{
public:
SubA(A& a) : A(a.m_pX) {};
};

When I compile this, I get an error stating that I can't access protected member A::m_pX.

Would someone be so kind as to 'splain this to me?


You can't access protected members of a class different from the one from
which you try to access the protected members.


Thanks!

Now I have a followup question :-)

I am trying to make a storable version of a class. For instance,

class A
{
public:
A(double value) {};

protected:
A() : m_value(0.0) {};
double m_value;
};

I want to make a storable version of A, so I thought I would write

class StorableA
{
public:
StorableA() {};
Load(Storage& s) {???};
Store(Storage& s) {???};
};

I want to be able to pull values from storage and populate the object. That
should be easy: just create a StorableA and populate it. Okay, now, given
an instance of A, I want to be able to store it. One way of doing this
would be to create a sort of copy constructor for StorableA.

class StorableA
{
public:
StorableA() {};
StorableA(A a) : m_value(a.m_value) {};
Load(Storage& s);
Store(Storage& s);
};

StorableA::Store(Storage& s)
{
s.Store(m_value);
}

This won't work because I can't access protected members of A.

Am I out of luck?

JAB

--
--------------------------------------------------------
John A. Byerly
Engineered Software Solutions, Ltd.
http://www.ess-quality.com
Jul 22 '05 #3
John A. Byerly wrote:

....
I am trying to make a storable version of a class. For instance,

class A
{
public:
A(double value) {};

protected:
A() : m_value(0.0) {};
double m_value;
};

I want to make a storable version of A, so I thought I would write

class StorableA
{
public:
StorableA() {};
Load(Storage& s) {???};
Store(Storage& s) {???};
};


If by "Storable" you mean serializable, then you might want to look at:

http://www.boost.org/libs/serialization/doc/index.html

Jeff Flinn
Jul 22 '05 #4
John A. Byerly wrote:
[...]
I am trying to make a storable version of a class. For instance,

class A
{
public:
A(double value) {};

protected:
A() : m_value(0.0) {};
double m_value;
};

I want to make a storable version of A, so I thought I would write

class StorableA
Did you mean

class StorableA : public A

?
{
public:
StorableA() {};
Load(Storage& s) {???};
Store(Storage& s) {???};
};

I want to be able to pull values from storage and populate the object. That
should be easy: just create a StorableA and populate it. Okay, now, given
an instance of A, I want to be able to store it. One way of doing this
would be to create a sort of copy constructor for StorableA.

class StorableA
{
public:
StorableA() {};
StorableA(A a) : m_value(a.m_value) {};
Load(Storage& s);
Store(Storage& s);
A nit pick: get into habit of writing proper C++, even if you're writing
to illustrate something. Here two things are not right: extraneous
semicolons and functions that don't have return value types.
};

StorableA::Store(Storage& s)
{
s.Store(m_value);
}

This won't work because I can't access protected members of A.
That's not true (clearly you didn't try it, otherwise you'd already know
that it's not true). You are allowed access to any member of _your_own_
subobject. IOW, you are allowed to access this->[anyprotectedmember].
Am I out of luck?


Of course not.

V
Jul 22 '05 #5

Okay, as Victor pointed out, there were problems with the code snippet I
supplied. Here is an updated version:

class A
{
public:
A(double value) : m_value(value) {}

protected:
A() : m_value(0.0) {}
double m_value;
};

As I said, I want to be able to store an instance of A, so I was hoping to
do so by creating a subclass:

class Storage
{
public:
void Store(double value) {}
};

class SubA : public A
{
public:
SubA(A* pA) : m_pA(pA) {}
void Store(Storage& s);
void Load(Storage& s);

private:
A* m_pA;
};

void SubA::Store(Storage& s)
{
s.Store(m_pA->m_value);
}

Now, I said before:
This won't work because I can't access protected members of A.
To which Victor replied:
That's not true (clearly you didn't try it, otherwise you'd already know
that it's not true). You are allowed access to any member of _your_own_
subobject. IOW, you are allowed to access this->[anyprotectedmember].


What I meant was through the instance of A. The Store() function above
should make this more clear.
Am I out of luck?


Of course not.


Please offer suggestions. I can't figure out how I can construct a fully
initialized instance of a subclass given an instance of the base class, so I
suspect my approach is flawed. But I haven't been able to come up with
another way to do this.

Thanks!

JAB

--
--------------------------------------------------------
John A. Byerly
Engineered Software Solutions, Ltd.
http://www.ess-quality.com
Jul 22 '05 #6
John A. Byerly wrote:
class A
{
public:
A(double value) : m_value(value) {}

protected:
A() : m_value(0.0) {}
double m_value;
};

As I said, I want to be able to store an instance of A, so I was hoping to
do so by creating a subclass:

class Storage
{
public:
void Store(double value) {}
};

class SubA : public A
{
public:
SubA(A* pA) : m_pA(pA) {}
void Store(Storage& s);
void Load(Storage& s);

private:
A* m_pA;
};

void SubA::Store(Storage& s)
{
s.Store(m_pA->m_value);
}
[snip]
Please offer suggestions. I can't figure out how I can construct a fully
initialized instance of a subclass given an instance of the base class, so
I suspect my approach is flawed. But I haven't been able to come up with


SubA (A * pA) : A (* pA) { }

And forget the m_pA member. The way you use does not initialize any A
instance, just stores a pointer to one.

--
Salu2
Jul 22 '05 #7
Julián Albo wrote:
And forget the m_pA member. The way you use does not initialize any A
instance, just stores a pointer to one.


Correction, initializes one instance, but not whith the parameter passed.

--
Salu2
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
2
by: MJ | last post by:
Hi I have a following sample code class base and class derived. I have inherited the base class as private and tried to compile the code its giving an error "conversion from 'class derived *' to...
6
by: Lionel | last post by:
Quick and easy question. If we have classes A, B and C. C extends/inherits B and B extends A. I have discovered C++ doesn't like C using protected members of A, what do I need to do so that C...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.