Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inheritance question

Reply
Thread Tools

inheritance question

 
 
hack_tick
Guest
Posts: n/a
 
      07-12-2004
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 ??


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      07-12-2004

"hack_tick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-12-2004
On Mon, 12 Jul 2004 12:05:19 +0530, hack_tick <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-12-2004
On Mon, 12 Jul 2004 12:10:36 +0530, Sharad Kala
<(E-Mail Removed)> wrote:

>
> "hack_tick" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>


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
 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      07-12-2004

"John Harrison" <(E-Mail Removed)> wrote in message
newspsa0igf06212331@andronicus...
> On Mon, 12 Jul 2004 12:10:36 +0530, Sharad Kala
> <(E-Mail Removed)> wrote:
>
>
> 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


 
Reply With Quote
 
hack_tick
Guest
Posts: n/a
 
      07-12-2004
hi there
"Sharad Kala" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
[...]
> 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


 
Reply With Quote
 
hack_tick
Guest
Posts: n/a
 
      07-12-2004
hi there,
"John Harrison" <(E-Mail Removed)> wrote in message
newspsa0idpq7212331@andronicus...
[...]
> 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-12-2004
On Mon, 12 Jul 2004 12:29:56 +0530, hack_tick <(E-Mail Removed)> wrote:

> hi there,
> "John Harrison" <(E-Mail Removed)> wrote in message
> newspsa0idpq7212331@andronicus...
> [...]
>> 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
 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      07-12-2004

"hack_tick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> hi there,
> "John Harrison" <(E-Mail Removed)> wrote in message
> 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.


 
Reply With Quote
 
hack_tick
Guest
Posts: n/a
 
      07-12-2004
hi there
"John Harrison" <(E-Mail Removed)> wrote in message
newspsa0jiqsx212331@andronicus...
[...]
> 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


 
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
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments