473,406 Members | 2,705 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,406 software developers and data experts.

Is this right? Can't call protected member of base class from derivedclass method, for another object

Basically this:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?

class C;

class B
{
friend class C;
....
Thanks,
Asfand Yar
--
http://www.it-is-truth.org/

Jul 19 '05 #1
17 2497
>
Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?


So, why is f protected? By making it protected, that one should not do
what you are trying to do. Makes no sense.

Anyway:
class B
{
protected:
void f() {}
public:
void g() {f();}
}

call g in C

--
Gabriel
Jul 19 '05 #2
Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bj**********@newsg4.svr.pol.co.uk>...
Basically this:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?
There is no way of doing it without the friend qualifier if you keep
f() protected. Protected members can be accessed only in methods of
its class or its class' descendants.

Make f() a public member and doit(B& arg) will be able to access it.

class C;

class B
{
friend class C;
...
Thanks,
Asfand Yar


Marcelo Pinto
Jul 19 '05 #3

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:bj**********@newsg4.svr.pol.co.uk...
Basically this:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can't do this (according to GCC 3.3.1).
No, obviously not. That's the whole poinf of making something protected.
Who designed B? If it was you, then there's something not quite right with
your design.
Any tips on how to
achieve this, considering I do not want to do something like the following?
class C;

class B
{
friend class C;


You can make only the function a friend. But again, why do you want to
break the design?
Jul 19 '05 #4
jeffc wrote:
"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:bj**********@newsg4.svr.pol.co.uk...
Basically this:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can't do this (according to GCC 3.3.1).

No, obviously not. That's the whole poinf of making something protected.
Who designed B? If it was you, then there's something not quite right with
your design.

Any tips on how to
achieve this, considering I do not want to do something like the


following?
class C;

class B
{
friend class C;

You can make only the function a friend. But again, why do you want to
break the design?


I forgot one VERY important detail. Typo. Sorry.

Here are the classes again:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C : public B
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};
I hope you see the typo corrected...........

--
http://www.it-is-truth.org/

Jul 19 '05 #5
Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bj**********@newsg4.svr.pol.co.uk>...
Basically this:
....
Any tips on how to
achieve this, considering I do not want to do something like the following?

class C;

class B
{
friend class C;
...


Yeah. Forget all about private/public/protected/friend/using,
make everything public, do your work.

Otherwise, you'll have make the classes friends.

stelios
Jul 19 '05 #6
Marcelo Pinto wrote:
There is no way of doing it without the friend qualifier if you keep
f() protected. Protected members can be accessed only in methods of
its class or its class' descendants.


Hi, thanks for the reply. Could you please refer to my reply jeffc
below? I made a typo in my original post, unfortunately - silly old
brain of mine! :-)


--
http://www.it-is-truth.org/

Jul 19 '05 #7
Gabriel Schreiber wrote:
Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?

So, why is f protected? By making it protected, that one should not do
what you are trying to do. Makes no sense.

Anyway:
class B
{
protected:
void f() {}
public:
void g() {f();}
}

call g in C


Hi, thanks for the reply. Could you please refer to my reply jeffc
below? I made a typo in my original post, unfortunately - silly old
brain of mine! :-)


--
http://www.it-is-truth.org/

Jul 19 '05 #8
Asfand Yar Qazi wrote:
Basically this:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C
^^class C : public B ^^^^^^^^^^^^^^^
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?

class C;

class B
{
friend class C;
....
Thanks,
Asfand Yar

Typo - whoops.

--
http://www.it-is-truth.org/

Jul 19 '05 #9
Asfand Yar Qazi wrote in news:bj**********@news8.svr.pol.co.uk:

I forgot one VERY important detail. Typo. Sorry.

Here are the classes again:

//file 'B.hh'
class B
{
protected:
void f() {}
static call_t( B & arg ) { arg.f(); }
};

//file 'C.hh'
#include "B.hh"
class C : public B
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
call_f( arg );
}
};


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #10

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:bj**********@news8.svr.pol.co.uk...

No, obviously not. That's the whole poinf of making something protected. Who designed B? If it was you, then there's something not quite right with your design.

Any tips on how to
achieve this, considering I do not want to do something like the


following?
class C;

class B
{
friend class C;

You can make only the function a friend. But again, why do you want to
break the design?


I forgot one VERY important detail. Typo. Sorry.

Here are the classes again:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C : public B
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};


Ah, well that is a more interesting question. However, the answer remains
the same. The reason is that the argument is not the same thing as the
object itself. For your code to run, 2 objects must exist. The first
object is the one that has "doit()" invoked on it. The second object is a
separate B object that is passed to doit() as a parameter. Inside doit, you
could defnitely do this:
void doit(B& arg)
{
f();
}

Unfortunately, that's not what you're doing. I know it seems strange, but
when you're not "inside" a B object, then any "outside" B object has to be
treated through its public interface only. In doit(), you are "inside" a B
object, but you're trying to access a different instance of an "outside" B
object.
Jul 19 '05 #11

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn*********************************@195.129.1 10.130...
Asfand Yar Qazi wrote in news:bj**********@news8.svr.pol.co.uk:

I forgot one VERY important detail. Typo. Sorry.

Here are the classes again:

//file 'B.hh'
class B
{
protected:
void f() {}


static call_t( B & arg ) { arg.f(); }


Typo? Besides, that looks a bit dodgy.
Jul 19 '05 #12
>
Ah, well that is a more interesting question. However, the answer remains
the same. The reason is that the argument is not the same thing as the
object itself. For your code to run, 2 objects must exist. The first
object is the one that has "doit()" invoked on it. The second object is a
separate B object that is passed to doit() as a parameter. Inside doit, you
could defnitely do this:
void doit(B& arg)
{
f();
}

Unfortunately, that's not what you're doing. I know it seems strange, but
when you're not "inside" a B object, then any "outside" B object has to be
treated through its public interface only. In doit(), you are "inside" a B
object, but you're trying to access a different instance of an "outside" B
object.


Hmm... I didn't know that. I do now.

Many thanks to all who replied.

--
http://www.it-is-truth.org/

Jul 19 '05 #13
jeffc wrote in news:3f********@news1.prserv.net:

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message

[snip]
> protected:
> void f() {}


static call_t( B & arg ) { arg.f(); }


Typo? Besides, that looks a bit dodgy.


Yep, it should have been ... call_f( ....

But what do you find "dodgy" about it ?

Would it perhapse be less dodgy if call_f() did what f() does
all by itself, and was maybe called f( B & arg ) ?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #14
Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bj**********@newsg4.svr.pol.co.uk>...
Basically this:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?

class C;

class B
{
friend class C;
...
Thanks,
Asfand Yar

In your subject you mentioned derived classes, but in the code you
gave C is not derived from B. That is why you can't access protected
members of B from C. If you declare C like this it should work:

#include "B.hh"
class C : public B // C is derived from B
{
// class C details
};
Jul 19 '05 #15

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...

But what do you find "dodgy" about it ?

Would it perhapse be less dodgy if call_f() did what f() does
all by itself, and was maybe called f( B & arg ) ?


Well, I guess it depends on how you view the original problem. I suppose if
you don't like the way the language was designed in this respect, then this
workaround can be accepted as a way to make it work differently Otherwise,
the original posted code really shouldn't work and the interface maybe
should be changed in a more standard way to accomplish what you want to
accomplish.
Jul 19 '05 #16

"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google.c om...

In your subject you mentioned derived classes, but in the code you
gave C is not derived from B. That is why you can't access protected
members of B from C. If you declare C like this it should work:

#include "B.hh"
class C : public B // C is derived from B
{
// class C details
};


Nope, not even then.
Jul 19 '05 #17

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:bj**********@news8.svr.pol.co.uk...

lines snipped
Here are the classes again:

//file 'B.hh'
class B
{
protected:
void f() {}
};

//file 'C.hh'
#include "B.hh"
class C : public B
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};
Ah, well that is a more interesting question. However, the answer remains
the same. The reason is that the argument is not the same thing as the
object itself. For your code to run, 2 objects must exist. The first
object is the one that has "doit()" invoked on it. The second object is a
separate B object that is passed to doit() as a parameter. Inside doit,

you could defnitely do this:
void doit(B& arg)
{
f();
}

Unfortunately, that's not what you're doing. I know it seems strange, but
when you're not "inside" a B object, then any "outside" B object has to be
treated through its public interface only. In doit(), you are "inside" a B object, but you're trying to access a different instance of an "outside" B
object.


I'm also trying to understand this. The above seems to imply it is based on
whether the invoking object
is the same as the object owning the method. At least according to
VisualC++ 7,
class B
{
protected:
void f() {}
virtual ~B(){}
};

class C : public B
{
public:
void doit(B& arg1, C &arg2)
{
// do some stuff
arg1.f(); //error
arg2.f(); //ok
(dynamic_cast<C*>(&arg1))->f();//ok
doit2(*this);
this->f();//ok
(dynamic_cast<C*>((dynamic_cast<B*>(this))))->f();//ok
(dynamic_cast<B*>(this))->f(); //error
}

private:
void doit2(C& arg) { };
};

int main()
{}

The instance wouldn't seem to matter from this. It also seems odd that a C
object can call
the protected B method of a different C object, but not of a different B
object.

What are the rules?


Jul 19 '05 #18

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

Similar topics

1
by: yan | last post by:
Hi, everybody. I'm new to this great forum, so hello! I start with a problem of my project. I have an Abstract Base Class used as base class for differentes Derived classes. The derived classes...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
6
by: Kylin | last post by:
Public Class test1 Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private...
7
by: atomik.fungus | last post by:
Hi, im having problems compiling some code, and i dont understand why. I've made a Matrix class for any kind of data and now im implementing an inherited class with all the mathematical stuff...
14
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef...
11
by: =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno | last post by:
I have a simple class: public class BaseClass { private string propertyA; protected string PropertyA { get { return propertyA; } set { propertyA = value; }
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.