Showing posts with label c/cpp. Show all posts
Showing posts with label c/cpp. Show all posts

Saturday, December 31, 2011

rate your c++ skills from 1 to 10

常常會聽到你對c++了解有多少
如果從1~10分
你會給你自己打幾分

自己大概列出了我心中的評分標準


  1. knows well about C
  2. knows well about OOP, operator overloading, exception handling
  3. knows about how to use STL
  4. knows well about template
  5. knows well about 'Design Pattern'
  6. knows well about the Design and Implementation of STL, Loki and Boost
  7. knows well about the whole language design
  8. knows well about Compiler Design and Implementations, Optimization, and Code-generation (Assembly)
  9. knows well how to design by using C++
  10. Master

Wednesday, November 23, 2011

function template

function template 與class template不同的地方在於

  1. function template有type deduction 
  2. function template 沒有 default template argument
  3. function template 沒有 partial specialization 

Thursday, November 10, 2011

out-of-class member template definition

在Modern C++ Design的第五章裡看到了


template
template
Functor::Functor(const Fun& fun)
: spImpl_(new FunctorHandler(fun));
{


}

這就是 out-of-class member template definition
前面的template  是給class的template
後面的template 是給function(cstor)的template

這也有類似的例子
http://stackoverflow.com/questions/5895179/syntax-of-out-of-class-definition-of-a-template-member-function-of-a-template-cl

Monday, November 7, 2011

stack frame alignment

除了structure alignment 外
還有stack frame alignment
http://stackoverflow.com/questions/8004458/compiler-g-seems-to-allocate-more-memory-for-instances-of-classes-than-it-ne


不過有篇post也討論為啥要在32bit上也要用16 byte
不過他好像沒討論出什麼!?
http://blogs.embarcadero.com/abauer/2010/01/14/38904

Wednesday, October 19, 2011

static_cast

c++的polymorphism 是透過pointer來達成的
i.e.

class parent {

};
class child {

};
child *c = new child;
parent *p = c;

可是把 child*c = new child ;  換成
child c;

polymophism就沒有了
這時可以用static_cast

parent &p = static_cast< parent>(c);

記得要用 & 變ref

不然只是多一個無關緊要的 p出來而已
印出他的address就知道了

Tuesday, October 4, 2011

c++的prefix ++ 跟postfix ++

在stackoverflow看到了pre-increment 跟post-increment的問題
http://stackoverflow.com/questions/7397740/post-increment-operator

才發現c++這跟c是不一樣的
prefix increment是return lvalue
postfix inc 是return rvalue

另外還有古老的com.std.c++的討論
http://groups.google.com/group/comp.std.c++/browse_thread/thread/7804d1fdf2f05a1c

回文裡面可是有幾位大師呢

Tuesday, May 24, 2011

c++的mutable

在看c++的時候
一直在想 mutable主要的用處 use-case 是什麼

這篇就講到了
http://stackoverflow.com/questions/105014/c-mutable-keyword

Monday, January 10, 2011

call a function before main

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
__attribute__((constructor))


for GCC on Solaris
#pragma init
http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Solaris-Pragmas.html#Solaris-Pragmas


如果是c++的話
當然 最簡單的就是在global scope定義一個object(或new)

Friday, December 17, 2010

cpp的member variable

member variable, or field
跟member function (or method)不一樣
不是存在v-table裡的
而是存在每個class裡

在 http://www.go4expert.com/forums/showthread.php?t=8403
的Exmaple 2裡
可以發現 dTest其實有另外存一份 Test的a 跟b
即使 a ,b 是private的
所以他的size是12

所以可以像Example 5一樣  加上 dTest 並來改 Test的a,b
Test 裡的 a ,b 是public的
所以也可以從dTest直接修改a b
像Example 5的例子 是用pointer來改
所以即使a, b是private的話也可以work

Friday, November 26, 2010

一個介紹 c++ vptr的好例子

http://www.go4expert.com/forums/showthread.php?t=8403

這個網頁從以前到現在 看了再看
都還是覺得很有趣

Thursday, November 25, 2010

c 的offsetof

在wiki上看到的offsetof的macro
http://en.wikipedia.org/wiki/Offsetof

http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Offsetof.html

http://blog.linux.org.tw/~jserv/archives/001399.html

而他有兩個argument
第一個是struct type 第二個是member

The C++ Programming Language的問題

1.在Chap3 3.11 Advice p.66 裡 第七條
Using string rather than char*

請問即使我對char * 用了簡單的strcat strcpy這些library
會比string 還要來的慢嗎???

或者是可以說明在處理字串時(就ascii就好了)
什麼時候用string,when to use char*呢?



2.在10.2.8 Structurs and Classes的最後 p.235
他講了一句
allowing many access sepcifiers in a class is useful for
machine generated code.
就是他上面有講個例子 public,private可以一直加的

class Data4 {
public :
 ...
private :
 ...
public:
...
}

這裡的machine generated code是指什麼?
是某些tool產生出的c++ code還是compile完產生的code
(IR, assembly, or machine code)
為什麼會useful??



3. 在10.4.6.2 Member Constants p249
他說可以initialize a static integral constant member
為什麼只有int 可以 float那些為什麼不行呢???



4.在11.2.3 p265
第一段要結束時他說
it is not possible to define an operator function that
operates exclusively on pointers



5.Sec13.4 Using Template Arguments to Specify Policy
主要在講用Template class來specify operation(less,eq,..)

在p339 倒數第三段最後面 他說了
whereas inlining a call through a pointer to function
requires exceptional attention from a compiler
在這裡指的exceptional attention是指什麼呢?

寫C寫久了會很習慣用pointer to function
但想知道這裡不建議使用pfn的原因







6. Sec14.2 Grouping of Exceptions
第一段的第三行有說
the effect of a throw is to unwind the stack until a
suitable catch is found

他說的unwind是什麼意思呢
是指一般function return時stack會清掉的意思嗎?
(esp回到ebp)
如果是這樣的話
throw 出來的那個object(or pointer,reference)
他是存在哪裡的??
stack被清掉那exception應該也會被清吧???

Friday, June 4, 2010

vptr 跟 pointer to member function

good article on vptr
http://www.dreamincode.net/forums/topic/45816-detail-about-how-vptr-and-virtual-table-works/


and 一兩年前的信


Hi
我昨天在看WebKitcode  有看到一行怪怪的code

(m_object->*m_function)(this);  //請注意  ->*

我本身對c++不熟 但是問了一些熟c++的朋友 他們也不是很清楚
回去看了Stroustup的書 也不是講的很清楚 只有一頁 ( 媽的爛書.....)
不過大概知道這是pointer to member function
pointer to member function 是一個比較少人知道c++特有的feature

不過有整理出一些point出來

1.  pointer to member function is NOT pointer to function

void (Test::*pmfn)(void)      void (*pfn)(void)       是不一樣的  即使他們的return type, signature是一樣的
另外也不能把他castgeneric pointer (void *)
可以參考 c++ lite faqpage
http://www.parashift.com/c++-faq-lite/pointers-to-members.html


2. pointer to member function is NOT a pointer to some address

一個pointer to member function , 他只是一個class offset的位置而已
( EX: c++ 有個virtual function table的東西 而這pointer to member function只是這table中的offset)
而不是指到Memory中某段的address
另外 他的sizeof 不一定是4(on 32bit), 有些compiler會給他8 bytelength

3. dereference pointer to member function 要用 .*  or ->*
這也就是我看到那行怪怪code的東西
. -> 的時機就跟C一樣 object pointer時就用 ->


然後我寫個test 給大家看看

#include
using namespace std;
class test {
    public:
        void func();
        void func1();
        void (test::*pmfn)();  //注意這是個 pointer to member function
                                        // 而且要加test:: ,如果沒有 他就只是一般的pointer to function
                                        // 就是下面那行的  (*pfn)()
        void (*pfn)();
        void call_pmfn(test&); //這是要用 test::*pmfn call function的一個wrapper
};
void test::func()
{
    cout<<"test::func\n";
}
void test::func1()
{
    cout<<"test::func1\n";
}
void func()
{
    cout<<"func\n";
}
void test::call_pmfn(test& t)
{
//    (this->*pmfn)();  //這就是call pointer to member function的地方
    (t.*pmfn)();          //在這裡我用兩個範例  , both can work
}
int main()
{
    test t;

    /* pointer to function */
    t.pfn = &func;  //這裡就是我們一般熟悉的pointer to function
    (t.pfn)();

    /* general pointer to member function */
    void (test::*pmfn1)() = &test::func1; //這裡是一般書上跟網頁上教你如何使用pointer to member function的東西
    (t.*pmfn1)();                                    //  請看待會後面的註解1

    /* pointer to member function
     * declared inside class, it is also a member of this class */
    t.pmfn = &test::func;

    //後記20090224  最後發現 (t.*t.pmfn)()也可以
    //(t.*pmfn)(); /*error here!  'pmfn' was not declared in this scope*/  
    //我在上面這行一直遇到compile error 這也是我的朋友們無法回答我問題的地方
    // 請看後面的註解2
    t.call_pmfn(t);
  
    return 0;
}


註解1
注意這個pmfn1 不是 test裡的member , 即使他有 test::開頭
因為他並沒有declaretest 裡面
其他的 test::func , test::func1可以definetest 外面  是因為他們也有在test 裡面被declared

加了test::開頭 是要告訴compile 這個pointer to member是要指到哪個class裡的member
compile 在這行 同時也會把test refernce傳給這個 pmfn1
(Recall第二點, pointer to member function 並沒有指到memory中的address
所以這時compiler要把class 的資訊告訴 pmfn1)


註解2
如果這裡 pmfn pmfn1一樣用 (t.*pmfn)()的話
是會有compile error出來的
大概原因(大概指的是我沒有很確定  我是從gdb猜的 而不是看gccsource code)
在這 compile沒有像  pmfn1一樣 有把testinformation pmfn
因為compiler在這就把pmfn當作一般的class member一樣
所以當 (t.*pfmn)()  ,compiler找不到pfmn是哪個classpointer to member /*這裡就是我不確定的地方*/
so compile will issue a error here

解決的辦法是
member function 跟一般的function有個不一樣的地方
就是他多了一個hiddenparameter , this
所以我作了一個wrapper member function
藉由this (也可以傳 test&進來) refernce call pointer to member function
也就是code裡的 test::call_pmfn()

不過至於pointer to member function什麼時候用
就要參考一些design pattern的書了

與大家分享一下C++特別的地方

thanks