Sponsored Content
Top Forums Programming Understand Virtual functions Internals Post 302571761 by rupeshkp728 on Tuesday 8th of November 2011 07:52:30 AM
Old 11-08-2011
Understand Virtual functions Internals

I am just trying to understand the virtual fns. concept.

I know that if I have a virtual fn. in a base class and its overridden fn. in derived class then based upon the address of base/derived object stored in the base class pointer the fns. will be called.


In the below code I had kept the show() fn. as virtual and it used to call properly the base and derived classes show().

But when I removed the virtual tag from show() then why does not it calls derived show() inspite of base class pointer having address of derived class object?


Code:
 
class base
{
public:
    base()
    {
        printf("Base Cons\n");
    }
 
    void show()
    {
        printf("Base Show\n");
    }
};
 
class der: public base
{
public:
    der()
    {
        printf("Der Cons\n");
    }
 
    void show()
    {
        printf("Der Show\n");
    }
};
 
void yahoo(base* bpointer)
{
    printf("Address in base pointer = %u\n",bpointer);
    bpointer->show();
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    class base bobj, *bptr;
    class der dobj;
 
    bptr = & bobj;
    printf("Base obj adress = %u\n",&bobj);
    yahoo(bptr);
 
    bptr = &dobj;
    printf("Base obj adress = %u\n",&dobj);
    yahoo(bptr);
 
    return 0;
}

Output of code:
Code:
Base Cons
Base Cons
Der Cons
Base obj adress = 1245027
Address in base pointer = 1245027
Base Show
Base obj adress = 1245003
Address in base pointer = 1245003
Base Show


Last edited by rupeshkp728; 11-08-2011 at 08:54 AM.. Reason: added tags around code
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

on unix internals

will anybody tell me how can i access all the fields of process table .if there is any structure and a system call please specify . (1 Reply)
Discussion started by: vish_shan
1 Replies

2. HP-UX

HP-UX Internals Book

. (2 Replies)
Discussion started by: Driver
2 Replies

3. UNIX for Dummies Questions & Answers

Have to log out of a virtual terminal twice in order to exit virtual terminals

Not really a newbie, but I have a strange problem and I'm not sure how to further troubleshoot it. I have to log out of a virtual terminal by typing exit, then exit again as in: woodnt@toshiba-laptop ~ $ exit logout woodnt@toshiba-laptop ~ $ exit logout I DON'T have to do this when I'm... (1 Reply)
Discussion started by: Narnie
1 Replies

4. AIX

Need help understand Virtual Processors

First of all I have performed a Google search and internal search and found several descriptions but nothing I can wrap my head around and feel 100% confident about. I feel really silly for asking this as I manage a P6 570 with 12 lpars but I have difficulity with Virtual Processors. I can... (3 Replies)
Discussion started by: juredd1
3 Replies

5. Shell Programming and Scripting

Don't understand how RS functions in awk

I learn using RS in awk to extract portion of file in this forum which is wonderful solution to the problem. However, I don't understand how exactly it operates. I don't quite understand the mechanism behind how searching for /DATA2/ can result in extracting the whole section under "DATA2" ... (3 Replies)
Discussion started by: joe228
3 Replies

6. UNIX for Dummies Questions & Answers

How can i understand if a Java Virtual Machine is installed on Unix??

Hello, i would like to figute out, if there is any JVM installed on my unix account. How can i figure that out?? Thanks (1 Reply)
Discussion started by: g_p
1 Replies

7. Solaris

Change hostID of Solaris 10 virtual/guest machine installed by Virtual Box 4.1.12 on Windows-XP host

Trying to set or modify the randomly set hostID of a Solaris 10 virtual/guest machine that I installed on a Windows-XP host machine (using Virtual Box 4.1.12). I was able to set/modify the hostname of the Solaris 10 virtual/guest machine during installation as well as via the Virtual Box... (4 Replies)
Discussion started by: Matt_VB
4 Replies

8. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

9. What is on Your Mind?

How to switch from SVR4/BSD internals to Linux internals?

Hello, Long-time Unix hacker here - I've worked on four variants of the kernel prior to the introduction of Linux. In my spare time, I've written Linux (Ubuntu) device drivers, kernel modules, cross-compiled, and built the kernel. I'd like to do Linux internals/device drivers as a day job,... (1 Reply)
Discussion started by: OriginalVersion
1 Replies

10. UNIX for Beginners Questions & Answers

Providing virtual machine priority in kvm based virtual machines

Hi All, Is there any way I can prioritize my VMs when there is resource crunch in host machine so that some VMs will be allocated more vcpu, more memory than other VMs in kvm/qemu hypervisor based virtual machines? Lets say in my cloud environment my Ubuntu 16 compute hosts are running some... (0 Replies)
Discussion started by: SanjayK
0 Replies
Mail::SPF::Base(3)					User Contributed Perl Documentation					Mail::SPF::Base(3)

NAME
Mail::SPF::Base - Base class for Mail::SPF classes SYNOPSIS
use base 'Mail::SPF::Base'; sub new { my ($class, @options) = @_; my $self = $class->SUPER::new(@options); ... return $self; } DESCRIPTION
Mail::SPF::Base is a common base class for all Mail::SPF classes. Constructor The following constructor is provided: new(%options): returns Mail::SPF::Base Creates a new object of the class on which the constructor was invoked. The provided options are stored as key/value pairs in the new object. The "new" constructor may also be called on an object, in which case the object is cloned. Any options provided override those from the old object. There are no common options defined in Mail::SPF::Base. Class methods The following class methods are provided: class: returns string Returns the class name of the class or object on which it is invoked. Class methods The following class methods are provided: make_accessor($name, $readonly): returns code-ref Creates an accessor method in the class on which it is invoked. The accessor has the given name and accesses the object field of the same name. If $readonly is true, the accessor is made read-only. Instance methods There are no common instance methods defined in Mail::SPF::Base. SEE ALSO
Mail::SPF For availability, support, and license information, see the README file included with Mail::SPF. AUTHORS
Julian Mehnle <julian@mehnle.net>, Shevek <cpan@anarres.org> perl v5.18.2 2017-10-06 Mail::SPF::Base(3)
All times are GMT -4. The time now is 03:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy