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/ 17 2427
> 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
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
"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?
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/
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
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/
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/
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/
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/
"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.
"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.
> 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/
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/
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
};
"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.
"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.
"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? This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by yan |
last post: by
|
reply
views
Thread by Chris F Clark |
last post: by
|
4 posts
views
Thread by matty.hall |
last post: by
|
6 posts
views
Thread by Kylin |
last post: by
|
7 posts
views
Thread by atomik.fungus |
last post: by
|
14 posts
views
Thread by mlimber |
last post: by
|
11 posts
views
Thread by =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno |
last post: by
|
15 posts
views
Thread by =?Utf-8?B?R2Vvcmdl?= |
last post: by
|
10 posts
views
Thread by blangela |
last post: by
| | | | | | | | | | | |