Sipdroid's Blog

interesting things about virtual function

Posted in c++ by sipdroid on April 25, 2010

class Base{

virtual void test1()
 {
printf("test1()in Base \n");
 }

virtual void test2(int x=5)
 {
 printf("test2(%d)in Base:\n",x);
 }

}

class Derived: public Base

{

virtual void test1(int x=6) // the virtual keyword is not needed
 {
printf("test1(%d)in Derived \n",x);
 }

virtual void test2(int x=6)   //the virtual keyword is not needed
 {
printf("test2(%d)in Derived\n",x);
 }

}

Use use;
 Base* base = &use;
 base->test1();   //print out is : test1() in Base, the reason is test1() in Base and Derived has different signature, no override.

base->test2(); //print out is : test2(5) in Derived the test2() in override, but default value does not have virtual property.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: