2012-01-19 64 views
15

在方法GetValues()使用互斥或以前拷貝構造dummy實例後發佈什麼因果關係 - 堆棧展開或返回值複製

class Protect 
{}; 

class Test 
{ 
public: 
    Protect GetValues() const; 

private: 
    Protect m_protVal; 
    Mutex m_mutex; 
}; 

Protect Test::GetValues() const 
{ 
    CLockGuard lock(m_mutex); 

    return m_protVal; 
} 

int main(int argc, char** argv) 
{ 
    Test myTestInstance; 

    Protect dummy = myTestInstance.GetValues(); 
} 

假設CLockGuardMutex是具備升壓LIB標準類。

+4

http://stackoverflow.com/questions/3856729/how-to -use-lock-guard-when-returning-protected-data – NPE

+0

你可以在調試器中一步一步地運行你的測試程序(例如Linux上的'gdb')來查找。 –

+2

@BasileStarynkevitch哪些人不會告訴他任何有關語言保證的內容。 –

回答