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

  • Thread starter Asfand Yar Qazi
  • Start date
A

Asfand Yar Qazi

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
 
G

Gabriel Schreiber

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
 
M

Marcelo Pinto

Asfand Yar Qazi said:
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
 
J

jeffc

Asfand Yar Qazi said:
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?
 
A

Asfand Yar Qazi

jeffc said:
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.




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...........
 
S

stelios xanthakis

Asfand Yar Qazi said:
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
 
A

Asfand Yar Qazi

Marcelo said:
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! :)
 
A

Asfand Yar Qazi

Gabriel said:
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! :)
 
A

Asfand Yar Qazi

Asfand said:
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.
 
R

Rob Williscroft

Asfand Yar Qazi wrote in
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.
 
J

jeffc

Asfand Yar Qazi said:
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.
 
A

Asfand Yar Qazi

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.
 
R

Rob Williscroft

jeffc wrote in
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.
 
M

Michael Klatt

Asfand Yar Qazi said:
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
};
 
J

jeffc

Rob Williscroft said:
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.
 
J

jeffc

Michael Klatt said:
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.
 
D

Douglas Stuart

lines snipped
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top