Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheritance: Can the base class access a function in the class whichis inheriting from it?

Reply
Thread Tools

Inheritance: Can the base class access a function in the class whichis inheriting from it?

 
 
ngoonee@gmail.com
Guest
Posts: n/a
 
      03-14-2008
Hi all,

My intent is to code some algorith which repeatedly applies a certain
comparison function in C++. The algorithm itself works fine, but I'm
searching for easy ways to select the comparison function to be
applied, preferably at run-time (ie. no preprocessor defines).

A very simplified example of the idea I had:-

class baseclass {
private:
virtual int compare(int a,int b);
public:
int do_something(int a, int b) {
return compare(a,b);
}
};

class class2ublic baseclass {
private:
int compare(int a,int b) {
return (b-a);
}
public:
};

class class1ublic baseclass {
private:
int compare(int a,int b) {
return (a-b);
}
public:
};

Hence, if I create an instance of class1 and call the function
do_something, I'd like it to access the compare() function within
class1. Obviously, however, it doesn't work as is, meaning the method
I'm trying is not legal.

1. Could anyone suggest a legal way of doing what I'm trying to
accomplish, besides simply copy-pasting the whole code of
do_something() into a seperate class?

2. Is there any other way besides class inheritance to accomplish the
above?

Thank you all.
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      03-14-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all,
>
> My intent is to code some algorith which repeatedly applies a certain
> comparison function in C++. The algorithm itself works fine, but I'm
> searching for easy ways to select the comparison function to be
> applied, preferably at run-time (ie. no preprocessor defines).
>
> A very simplified example of the idea I had:-
>
> class baseclass {
> private:
> virtual int compare(int a,int b);
> public:
> int do_something(int a, int b) {
> return compare(a,b);
> }
> };
>
> class class2ublic baseclass {
> private:
> int compare(int a,int b) {
> return (b-a);
> }
> public:
> };
>
> class class1ublic baseclass {
> private:
> int compare(int a,int b) {
> return (a-b);
> }
> public:
> };
>
> Hence, if I create an instance of class1 and call the function
> do_something, I'd like it to access the compare() function within
> class1. Obviously, however, it doesn't work as is, meaning the method
> I'm trying is not legal.
>
> 1. Could anyone suggest a legal way of doing what I'm trying to
> accomplish, besides simply copy-pasting the whole code of
> do_something() into a seperate class?
>
> 2. Is there any other way besides class inheritance to accomplish the
> above?
>

What doesn't work?
The only problem that I can see is that you haven't implemented
baseclass::compare. Either provide an implmeentation or declare it as
pure virtual.

 
Reply With Quote
 
 
 
 
ngoonee@gmail.com
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 1:23 pm, red floyd <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Hi all,

>
> > My intent is to code some algorith which repeatedly applies a certain
> > comparison function in C++. The algorithm itself works fine, but I'm
> > searching for easy ways to select the comparison function to be
> > applied, preferably at run-time (ie. no preprocessor defines).

>
> > A very simplified example of the idea I had:-

>
> > class baseclass {
> > private:
> > virtual int compare(int a,int b);
> > public:
> > int do_something(int a, int b) {
> > return compare(a,b);
> > }
> > };

>
> > class class2ublic baseclass {
> > private:
> > int compare(int a,int b) {
> > return (b-a);
> > }
> > public:
> > };

>
> > class class1ublic baseclass {
> > private:
> > int compare(int a,int b) {
> > return (a-b);
> > }
> > public:
> > };

>
> > Hence, if I create an instance of class1 and call the function
> > do_something, I'd like it to access the compare() function within
> > class1. Obviously, however, it doesn't work as is, meaning the method
> > I'm trying is not legal.

>
> > 1. Could anyone suggest a legal way of doing what I'm trying to
> > accomplish, besides simply copy-pasting the whole code of
> > do_something() into a seperate class?

>
> > 2. Is there any other way besides class inheritance to accomplish the
> > above?

>
> What doesn't work?
> The only problem that I can see is that you haven't implemented
> baseclass::compare. Either provide an implmeentation or declare it as
> pure virtual.



Perhaps I wasn't being clear enough, apologies. I don't want to (in
any situation) use baseclass::compare. I will not be creating any
objects of class baseclass, only of class1 and class2. I would like
class1 and class2 to be able to use baseclass's do_something()
function, but using their own unique versions of compare().

Thank you for your prompt response.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-14-2008
(E-Mail Removed) wrote:
> On Mar 14, 1:23 pm, red floyd <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> Hi all,
>>> My intent is to code some algorith which repeatedly applies a certain
>>> comparison function in C++. The algorithm itself works fine, but I'm
>>> searching for easy ways to select the comparison function to be
>>> applied, preferably at run-time (ie. no preprocessor defines).
>>> A very simplified example of the idea I had:-
>>> class baseclass {
>>> private:
>>> virtual int compare(int a,int b);
>>> public:
>>> int do_something(int a, int b) {
>>> return compare(a,b);
>>> }
>>> };
>>> class class2ublic baseclass {
>>> private:
>>> int compare(int a,int b) {
>>> return (b-a);
>>> }
>>> public:
>>> };
>>> class class1ublic baseclass {
>>> private:
>>> int compare(int a,int b) {
>>> return (a-b);
>>> }
>>> public:
>>> };
>>> Hence, if I create an instance of class1 and call the function
>>> do_something, I'd like it to access the compare() function within
>>> class1. Obviously, however, it doesn't work as is, meaning the method
>>> I'm trying is not legal.
>>> 1. Could anyone suggest a legal way of doing what I'm trying to
>>> accomplish, besides simply copy-pasting the whole code of
>>> do_something() into a seperate class?
>>> 2. Is there any other way besides class inheritance to accomplish the
>>> above?

>> What doesn't work?
>> The only problem that I can see is that you haven't implemented
>> baseclass::compare. Either provide an implmeentation or declare it as
>> pure virtual.

>
>
> Perhaps I wasn't being clear enough, apologies. I don't want to (in
> any situation) use baseclass::compare. I will not be creating any
> objects of class baseclass, only of class1 and class2. I would like
> class1 and class2 to be able to use baseclass's do_something()
> function, but using their own unique versions of compare().
>


Then declare it pure virtual...
Note the "= 0" in the class below.

What you have created is a well known design pattern called "Template"
(not to be confused with C++ templates).

class baseclass {
private:
virtual int compare(int a,int b) = 0;
public:
int do_something(int a, int b) {
return compare(a,b);
}
};
 
Reply With Quote
 
ngoonee@gmail.com
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 1:41 pm, red floyd <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Mar 14, 1:23 pm, red floyd <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >>> Hi all,
> >>> My intent is to code some algorith which repeatedly applies a certain
> >>> comparison function in C++. The algorithm itself works fine, but I'm
> >>> searching for easy ways to select the comparison function to be
> >>> applied, preferably at run-time (ie. no preprocessor defines).
> >>> A very simplified example of the idea I had:-
> >>> class baseclass {
> >>> private:
> >>> virtual int compare(int a,int b);
> >>> public:
> >>> int do_something(int a, int b) {
> >>> return compare(a,b);
> >>> }
> >>> };
> >>> class class2ublic baseclass {
> >>> private:
> >>> int compare(int a,int b) {
> >>> return (b-a);
> >>> }
> >>> public:
> >>> };
> >>> class class1ublic baseclass {
> >>> private:
> >>> int compare(int a,int b) {
> >>> return (a-b);
> >>> }
> >>> public:
> >>> };
> >>> Hence, if I create an instance of class1 and call the function
> >>> do_something, I'd like it to access the compare() function within
> >>> class1. Obviously, however, it doesn't work as is, meaning the method
> >>> I'm trying is not legal.
> >>> 1. Could anyone suggest a legal way of doing what I'm trying to
> >>> accomplish, besides simply copy-pasting the whole code of
> >>> do_something() into a seperate class?
> >>> 2. Is there any other way besides class inheritance to accomplish the
> >>> above?
> >> What doesn't work?
> >> The only problem that I can see is that you haven't implemented
> >> baseclass::compare. Either provide an implmeentation or declare it as
> >> pure virtual.

>
> > Perhaps I wasn't being clear enough, apologies. I don't want to (in
> > any situation) use baseclass::compare. I will not be creating any
> > objects of class baseclass, only of class1 and class2. I would like
> > class1 and class2 to be able to use baseclass's do_something()
> > function, but using their own unique versions of compare().

>
> Then declare it pure virtual...
> Note the "= 0" in the class below.
>
> What you have created is a well known design pattern called "Template"
> (not to be confused with C++ templates).
>
> class baseclass {
> private:
> virtual int compare(int a,int b) = 0;
> public:
> int do_something(int a, int b) {
> return compare(a,b);
> }
>
> };


Thank you. I tried this but in itself it did not solve my problem. The
issue was with me, however, and my incomplete understanding of
polymorphism. It turns out that I shouldn't create an instance of
class1 or class2 per se, instead I should have done the below:-

// code fragment

baseclass * bclass_ptr = new class1();
int result = (*bclass_ptr).do_something(1,5);

// end code fragment

Thank you for your help, red floyd sir. Any further advise on the
topic of polymorphism would be appreciated, though my immediate
problem has been solved.
 
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
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
Cannot Access Base Members when Inheriting WebControl TJO ASP .Net 2 03-16-2006 02:35 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Designer Support when Inheriting from a Base Page Class thechaosengine ASP .Net 3 08-24-2005 12:28 PM
Microsoft becoming a gangster? or maybe a common mugger....whichis worse I wonder..... Matthew Poole NZ Computing 4 05-01-2005 10:13 PM



Advertisments