Sponsored Content
Full Discussion: C++ overriding Vs hiding
Top Forums Programming C++ overriding Vs hiding Post 302342671 by Corona688 on Monday 10th of August 2009 11:42:23 AM
Old 08-10-2009
Consider this code:
Code:
#include <stdio.h>

class B
{
public:
        void fns(void){ fprintf(stderr, "B::fns\n"); }
};

class D:public B
{
public:
        void fns(void) {fprintf(stderr, "D::fns\n"); }
};

void call_fns(B &obj)   {       obj.fns();      }

int main(void)
{
        D d;
        call_fns(d);
}

It will print "B::fns" because when the object is cast into its parent "B", the compiler no longer knows it should be calling D::fns. Without virtual functions, the compiler calls member functions based on the class type alone; casting it into its parent class loses all the functions you overrode.

But when we do this:
Code:
#include <stdio.h>

class B
{
public:
        virtual void fns(void){ fprintf(stderr, "B::fns\n"); }
};

class D:public B
{
public:
        virtual void fns(void) {fprintf(stderr, "D::fns\n"); }
};

void call_fns(B &obj)   {       obj.fns();      }

int main(void)
{
        D d;
        call_fns(d);
}

This time it will print D::fns. Virtual tells the compiler to remember if a function has been overloaded, even if it is cast into a parent class. It does so with a hidden variable, so keep in mind that using virtuals has a small performance cost, and increases the size of the class.
 

10 More Discussions You Might Find Interesting

1. AIX

overriding function calls without recompiling

i want to replace the *alloc and free function calls in an existing project with my own functions, to be able to log the adresses etc in a text file. (memoryleak debugging) I think LD_PRELOAD is what i am looking for. That way i could create a Library with my own malloc functions and link them... (1 Reply)
Discussion started by: Lazzar
1 Replies

2. IP Networking

overriding /etc/resolv.conf with .nslookuprc file

Hi, I have come across a fact that /etc/resolv.conf can be overrided by using ~/.nslookuprc. Is this completely true...i.e can I completely override /etc/resolv.conf. If so, can I have a list of my own domainnames & Nameservers in ~/.nslookuprc that can override those in resolv.conf. ... (3 Replies)
Discussion started by: smanu
3 Replies

3. Programming

overriding the dynamic library

Hi, I wonder how can we override the dynamic library loaded by ld on start up.(dynamic linked application). so that linker uses the new library to find symbols. Is it possible to do. Cheers. (4 Replies)
Discussion started by: Raom
4 Replies

4. UNIX for Dummies Questions & Answers

overriding the mask in setfacl

hello everbody: Im trying to give the user "ydarwish" a full access over some directory on my sol9 machine. however the setfacl is recalculating the mask parameter keeping me from keeping him effective write and read access: root@Obi-Wan> setfacl -m u:ydarwish:rwx /IN_ARCHIVE6 root@Obi-Wan>... (4 Replies)
Discussion started by: aladdin
4 Replies

5. Shell Programming and Scripting

Overriding PATH

Hello, I'm using Sun solaris . I'm trying to override the environmental variable in my script, however when I execute the script, the PATH whatever being set in .profile is taking precedence. I have done the sanity checks like order of my entry in path, permissions for the user etc., To... (1 Reply)
Discussion started by: brainyoung
1 Replies

6. UNIX for Dummies Questions & Answers

Hiding Password

Hello. A bit of a puzzle here: I have a 3rd party executable, which requires the following parameters: parm1 = program_name, parm2=userid/password, parm3=additional flags. We tried passing password as a variable, but you can do grep, and see what the password actually is I found a bit... (2 Replies)
Discussion started by: Kishinevetz
2 Replies

7. UNIX for Dummies Questions & Answers

ftp - check if file on remote exists (skip overriding)

Hello, I have a script that uploads a file from local to remote place using ftp. The problem is that, if on remote host there is a file called the same as the one I want to upload, the ftp program overrides that file. But I don't want to override nothing (even if the remote file is older,... (3 Replies)
Discussion started by: spiriad
3 Replies

8. Shell Programming and Scripting

Overriding XML File

Hi All, I have an XML file (normally >3mb). I need to loop through this and override with some new (correct) values. Here is a row of data:- <row> <field name="ID">1</field> <field name="Type">a</field> <field name="value1">xxx</field> <field name="value2">xxxx</field> ....... (3 Replies)
Discussion started by: robfwauk
3 Replies

9. UNIX for Advanced & Expert Users

Overriding Mailx Command

Hello All, I am working on a project where the requirement is override mailx command in such way that, instead of sending mailing to email addresses coded in codes, it should send mails to one common email address at run time. We do not intend to change the email addresses in codes. This... (1 Reply)
Discussion started by: shubh05
1 Replies

10. Shell Programming and Scripting

[Solved] Help with Overriding a Prompt in UNIX/Java

I am executing a shell script which contains a jar call to an external java package for which I don’t have a read access to. The external package was written in such a way that whenever we make a java –jar call to the package, it shows a prompt on the console asking if we want to continue or no... (1 Reply)
Discussion started by: Harry1302
1 Replies
RECURSIVEDIRECTORYITERATOR(3)						 1					     RECURSIVEDIRECTORYITERATOR(3)

The RecursiveDirectoryIterator class

INTRODUCTION
The RecursiveDirectoryIterator provides an interface for iterating recursively over filesystem directories. CLASS SYNOPSIS
RecursiveDirectoryIterator RecursiveDirectoryIteratorextends FilesystemIteratorSeekableIteratorRecursiveIterator Methods o public RecursiveDirectoryIterator::__construct (string $path, [int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemItera- tor::CURRENT_AS_FILEINFO]) o public mixed RecursiveDirectoryIterator::getChildren (void ) o public string RecursiveDirectoryIterator::getSubPath (void ) o public string RecursiveDirectoryIterator::getSubPathname (void ) o public bool RecursiveDirectoryIterator::hasChildren ([bool $allow_links = false]) o public string RecursiveDirectoryIterator::key (void ) o public void RecursiveDirectoryIterator::next (void ) o public void RecursiveDirectoryIterator::rewind (void ) Inherits o public FilesystemIterator::__construct (string $path, [int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CUR- RENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS]) o public mixed FilesystemIterator::current (void ) o public int FilesystemIterator::getFlags (void ) o public string FilesystemIterator::key (void ) o public void FilesystemIterator::next (void ) o public void FilesystemIterator::rewind (void ) o public void FilesystemIterator::setFlags ([int $flags]) CHANGELOG
+--------------+---------------------------------------------------+ | Version | | | | | | | Description | | | | +--------------+---------------------------------------------------+ | 5.3.0 | | | | | | | The FilesystemIterator was introduced as the | | | parent class. Previously, the parent was the | | | DirectoryIterator. | | | | | 5.3.0 | | | | | | | Implements SeekableIterator. | | | | |5.2.11, 5.3.1 | | | | | | | Added RecursiveDirectoryIterator::FOLLOW_SYMLINKS | | | | +--------------+---------------------------------------------------+ PHP Documentation Group RECURSIVEDIRECTORYITERATOR(3)
All times are GMT -4. The time now is 09:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy