Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this right? Can't call protected member of base class from derivedclass method, for another object

Reply
Thread Tools

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

 
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      09-09-2003
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/

 
Reply With Quote
 
 
 
 
Gabriel Schreiber
Guest
Posts: n/a
 
      09-09-2003
>
> 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
 
Reply With Quote
 
 
 
 
Marcelo Pinto
Guest
Posts: n/a
 
      09-09-2003
Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bjk06p$26d$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      09-09-2003

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:bjk06p$26d$(E-Mail Removed)...
> 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?


 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      09-09-2003
jeffc wrote:
> "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
> news:bjk06p$26d$(E-Mail Removed)...
>
>>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/

 
Reply With Quote
 
stelios xanthakis
Guest
Posts: n/a
 
      09-09-2003
Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bjk06p$26d$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      09-09-2003
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/

 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      09-09-2003
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/

 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      09-09-2003
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/

 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      09-09-2003
Asfand Yar Qazi wrote in news:bjkqnj$96q$(E-Mail Removed):

>
> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't take pointer-to-member of protected member of base class K. Frank C++ 8 03-22-2012 06:50 PM
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
When is a base class protected member not visible in a derived class? Andy Lomax C++ 5 06-30-2005 04:34 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM



Advertisments