LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 09: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, 09:48 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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 09:54 AM.
 
Old 12-21-2009, 10: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 10:16 AM.
 
Old 12-22-2009, 10:17 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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 10:18 AM.
 
  


Reply



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 09:04 PM
need proprietary alternative to GPL-licensed functions in linux/drivers/base/class.c lightdee Linux - Kernel 2 09-05-2009 01:20 PM
C++: derived class? unihiekka Programming 4 02-03-2009 09:40 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:34 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
Open Source Consulting | Domain Registration