inheritance question

H

hack_tick

hi there
I m having a class A

class A
{
private:
virtual void foo();
};

and class B

class B : public A
{
protected:
void foo();
}

then i reimplement my version of foo
the problem i m facing is, i also require functionality of foo() from the
base class A, if the foo() would have been declared as protected then i
would have been able to access it, but since its private i cannot access it
after inheritance

i would like to do something similar to this

void B::foo()
{
/*my own functionality*/
A::foo();
}

any suggestion ??
 
S

Sharad Kala

hack_tick said:
hi there
I m having a class A

class A
{
private:
virtual void foo();
};

and class B

class B : public A
{
protected:
void foo();
}
Template pattern.

class A
{
public:
void f() {A::foo(); foo();}
private:
virtual void foo(){cout << "Base foo";}
};


class B : public A
{
protected:
void foo(){cout << "Derived foo";}
};

int main()
{
A* b = new B;
b->f();
delete b;
}

-Sharad
 
J

John Harrison

hi there
I m having a class A

class A
{
private:
virtual void foo();
};

and class B

class B : public A
{
protected:
void foo();
}

then i reimplement my version of foo
the problem i m facing is, i also require functionality of foo() from the
base class A, if the foo() would have been declared as protected then i
would have been able to access it, but since its private i cannot access
it
after inheritance

i would like to do something similar to this

void B::foo()
{
/*my own functionality*/
A::foo();
}

any suggestion ??


The only possibility I know of is the old hack

#define private protected

but that breaks every C++ rule in the book (well a few of them anyway) and
therefore is not guaranteed to work.

Presumably the author of class A had a good reason for declaring foo
private. They had some reason why what you are trying to do is a bad idea.
I suggest you talk to them.

john
 
J

John Harrison

Template pattern.

class A
{
public:
void f() {A::foo(); foo();}
private:
virtual void foo(){cout << "Base foo";}
};


class B : public A
{
protected:
void foo(){cout << "Derived foo";}
};

int main()
{
A* b = new B;
b->f();
delete b;
}

-Sharad

I assumed that the OP wasn't in a position to modify class A. Otherwise
s/he would have just changed private to protected.

john
 
S

Sharad Kala

John Harrison said:
On Mon, 12 Jul 2004 12:10:36 +0530, Sharad Kala


I assumed that the OP wasn't in a position to modify class A. Otherwise
s/he would have just changed private to protected.

Yep, I realized that too after reading your reply. But frankly that's the
last thing or may be just not the right thing (and I think you agree too)
to solve the problem.

-Sharad
 
H

hack_tick

hi there
[...]
Template pattern.

class A
{
public:
void f() {A::foo(); foo();}
private:
virtual void foo(){cout << "Base foo";}
};


class B : public A
{
protected:
void foo(){cout << "Derived foo";}
};

int main()
{
A* b = new B;
b->f();
delete b;
}

thank you for your reply, one thing i didnt mentioned in my previous post
was class "A" is a SDK header file, so modifying it is not the solution :)

-regards,
and once again thx for such a quick response :)
 
H

hack_tick

hi there,
[...]
The only possibility I know of is the old hack

#define private protected

but that breaks every C++ rule in the book (well a few of them anyway) and
therefore is not guaranteed to work.

Presumably the author of class A had a good reason for declaring foo
private. They had some reason why what you are trying to do is a bad idea.
I suggest you talk to them.

thankx for the reply that looks like a viable hack for now, actually the
function is declared "virtual" in the base class, i dont know why did the
author kept it virtual if he dosnt want it to be overridden :-( is there any
other use of "virtual" keyword for the "member functions" ???

regards :)
 
J

John Harrison

hi there,
[...]
The only possibility I know of is the old hack

#define private protected

but that breaks every C++ rule in the book (well a few of them anyway)
and
therefore is not guaranteed to work.

Presumably the author of class A had a good reason for declaring foo
private. They had some reason why what you are trying to do is a bad
idea.
I suggest you talk to them.

thankx for the reply that looks like a viable hack for now, actually the
function is declared "virtual" in the base class, i dont know why did the
author kept it virtual if he dosnt want it to be overridden :-( is there
any
other use of "virtual" keyword for the "member functions" ???

He does want it to be overridden, and if you override it your overridden
function will be called. But he only wants it to be called from within
class A, not from within a derived class.

Why that might be I couldn't say without knowing more about the classes.

I might add, how do you know what A::foo does? If it's a private function
why has it been documented (that is strange). Perhaps one reason it is
private is that the author wants to reserve the right to change it in a
non-backwards compatible way in the future. Are you sure you need to call
A:foo?

john
 
S

Sharad Kala

hack_tick said:
hi there,
thankx for the reply that looks like a viable hack for now, actually the
function is declared "virtual" in the base class, i dont know why did the
author kept it virtual if he dosnt want it to be overridden :-( is there any
other use of "virtual" keyword for the "member functions" ???

Access privilege and virtualness are independent of each other. A private
virtual function means that derived classes may or may not override it as
they choose, however they can not invoke its implementation.
 
H

hack_tick

hi there
[...]
He does want it to be overridden, and if you override it your overridden
function will be called. But he only wants it to be called from within
class A, not from within a derived class.

Why that might be I couldn't say without knowing more about the classes.

I might add, how do you know what A::foo does? If it's a private function
why has it been documented (that is strange). Perhaps one reason it is
private is that the author wants to reserve the right to change it in a
non-backwards compatible way in the future. Are you sure you need to call
A:foo?

Actually i m overwriding default Eventhandler provided with my Platform SDK
which is triggered by the framework, i dont know the exact way the funcion
is implemented but i have the rough Idea what it does, the other alternative
would be to duplicate the code what it does, but that would lead to another
issues of testing the code, so i was looking for some way to maintian the
basic functionality of what the basic code does,

i m basically preparing log of all the events triggered(testing the OOP SDK)
and also maintaining the dafault functionality of the SDK methods :)

regards
 
J

JKop

hack_tick posted:
hi there
[...]
Template pattern.

class A
{
public:
void f() {A::foo(); foo();} private:
virtual void foo(){cout << "Base foo";} };


class B : public A { protected:
void foo(){cout << "Derived foo";} };

int main()
{
A* b = new B;
b->f();
delete b;
}

thank you for your reply, one thing i didnt mentioned in my previous
post was class "A" is a SDK header file, so modifying it is not the
solution :)

-regards,
and once again thx for such a quick response :)


copy blah.h my_blah.h

edit my_blah.h

#include <my_blah.h>


-JKop
 

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