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

Synchronized Age Map

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

I often need a synchronized map in multiple threads environment. developed a basic one , if  you have any comments to improve it, please let me know.


/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 pthread_mutexattr_t mutex_attr;
 pthread_mutexattr_init(&mutex_attr);
 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
 pthread_mutex_init(&mutex_, &mutex_attr);
 // pthread_mutex_init(&mutex_, NULL);

Tagged with: