By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
17 Replies


P: n/a
>
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
>
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.