Visit Our UNIX and Linux User Community


C++ overriding Vs hiding


 
Thread Tools Search this Thread
Top Forums Programming C++ overriding Vs hiding
# 1  
Old 08-10-2009
Question C++ overriding Vs hiding

Code:
class B
{
public:
void fns(void){//base def;}
};
class D:public B
{
public:
void fns(void) {//new def;}
};

I was thinking the above is overriding but somewhere else i found the above is just hiding.Only virtual functions can be considered as overriding?
This is the exact statement
Code:
Overriding has different scopes, same name, same signatures, and virtual is required.

Please explain
# 2  
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #460
Difficulty: Medium
The 64-bit timestamps used by NTP consist of a 32-bit part for seconds and a 32-bit part for fractional second.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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

Featured Tech Videos