LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-21-2009, 10:31 AM   #1
caruna
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Rep: Reputation: 0
HELP:: Can I have virtual functions in derived class?? < Are they restricted to Base>


Please reffer below ::

class Base{

public:
Base(){ printf("Base class condructr invoked\n");}

void execute();

int common();

~Base() { printf("Base detructor\n");}

};


void Base::execute(){



int x=0;


x=common();

printf("%d \n value \n",x);

}


int Base::common(){

int m = 10;

return 10;
}




In the above example code I want to use the execute function of the base class & local variables, this Base class is used in lots of place meant lots of classes have derived this.


Now I want to use this Base class and when execute is callled I wante my common funtion to be called, wanted to know whether
it is feasible to change the base class function name common to virtual<this will not impact any other code>

or to do the following way??


class Derived:: public Base{

public:

virtual int common();
}

int Derived :: common(){

return 20;
}



can I declare function by virtual in the derived class is this accetable??

since I am new to C++ devell.. I i dont want mess up with exising large chunk of code..I hope I above is clear


please do suggest, thanks in advance
 
Old 12-21-2009, 10:48 AM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by caruna View Post
wanted to know whether
it is feasible to change the base class function name common to virtual<this will not impact any other code>
I can't figure out what the above phrase means, so I can't understand your question.

But here is an answer in the general area of your question:

If a function is declared virtual in a derived class and not virtual in the base class, any call to that function where the compile time type is the base class (such as any call through a pointer to base class) will use the base class function. Any call where the compile time type is the derived class (or a class further derived from that) will go through the vtable to get the appropriate virtual function for the run time type.

So, if I understand your example correctly, you must change the base class definition of common() to be virtual in order for the base class version of execute() to call the run time type version of common(). Declaring the derived class common() as virtual isn't enough if the base class common isn't virtual and isn't necessary if the base class common is virtual.

This will add a little overhead to all objects of that base class and any derived classes, but it should not change the results for any base class object, nor for any other derived classes that don't declare an override for common().

Last edited by johnsfine; 12-21-2009 at 10:54 AM.
 
Old 12-21-2009, 11:08 AM   #3
caruna
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thnaks for the reply,

from johnsfine
Quote:
Originally Posted by caruna
wanted to know whether
it is feasible to change the base class function name common to virtual<this will not impact any other code>

I can't figure out what the above phrase means, so I can't understand your question.


Wanted to know, whether the existing functionality will get impacted by changing the base class function to virtual?? I believe it will not get impacted for other code which using this base class.

Thanks...

Last edited by caruna; 12-21-2009 at 11:16 AM.
 
Old 12-22-2009, 11:17 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Wanted to know, whether the existing functionality will get impacted by changing the base class function to virtual?? I believe it will not get impacted for other code which using this base class.
It will probably not affect other code, particularly if yours is the only class that overrides the common function. If there are no other virtual functions, it will affect the layout of base class objects (which wouldn't be a problem under most circumstances).

However, you do need to be careful that your common code does not omit anything that is required 'by contract' in the original class (either by calling the base class common from the common function in the derived class, or replacing the functionality in the derived class). You also need to determine whether the destructor should also be virtual (this will depend on how the objects are deleted).

You will also need to recompile any code that uses the base class.

You wouldn't simply declare the common function as virtual in the derived class, since the base class will still have a non-virtual common function (I am assuming that your intention is to override common where other code is using a pointer to an object of the base class type).

Last edited by neonsignal; 12-22-2009 at 11:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
declare virtual function in derived class icecubeflower Programming 3 11-28-2009 10:04 PM
need proprietary alternative to GPL-licensed functions in linux/drivers/base/class.c lightdee Linux - Kernel 2 09-05-2009 02:20 PM
C++: derived class? unihiekka Programming 4 02-03-2009 10:40 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 12:35 PM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 07:33 PM


All times are GMT -5. The time now is 01:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration