青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 9, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

2013年5月17日

  • Make sure operator= is well-behaved when an object is assigned to itself. Techniques include comparing addresses of source and target objects, careful statement ordering, and copy-and-swap.

  • Make sure that any function operating on more than one object behaves correctly if two or more of the objects are the same.

posted @ 2013-05-17 15:00 魏尚堂 閱讀(162) | 評論 (0)編輯 收藏

This is only a convention; code that doesn't follow it will compile. However, the convention is followed by all the built-in types as well as by all the types in  the standard library (e.g., string, vector, complex, tr1::shared_ptr, etc.). Unless you have a good reason for doing things differently, don't.

posted @ 2013-05-17 12:06 魏尚堂 閱讀(165) | 評論 (0)編輯 收藏

base class constructors execute before derived class constructors, derived class data members have not been initialized when base class constructors run. If virtual functions called during base class construction went down to derived classes, the derived class functions would almost certainly refer to local data members, but those data members would not yet have been initialized.Calling down to parts of an object that have not yet been initialized is inherently dangerous, so C++ gives you no way to do it.

#include <iostream>
#include <string>
#include <cstdlib>
void print(std::string str){std::cout << str<< std::endl;}
class Transaction {
    public:
        Transaction()
        {
            print("Transaction Constructor");
            logTransaction();
        }
        virtual void logTransaction() const // =0;
        {
            print("Transaction Log");
        }
};
class BuyTransaction: public Transaction
{
    public:
        BuyTransaction(){   print("BuyTransaction Constructor");}
        virtual void logTransaction() const
        {
            print("BuyTransaction Log");
        }
};
int main()
{
    BuyTransaction dbc;
    //dbc.logTransaction();
}
pure virtual functions cannot link.
[shangtang@BTSOM-1 study]$ g++ TestT.cpp
TestT.cpp: In constructor 'Transaction::Transaction()':
TestT.cpp:14: warning: abstract virtual 'virtual void Transaction::logTransaction() const' called from constructor
/tmp/ccXFzaHv.o: In function `Transaction::Transaction()':
TestT.cpp:(.text._ZN11TransactionC2Ev[Transaction::Transaction()]+0x7f): undefined reference to `Transaction::logTransaction() const'
collect2: ld returned 1 exit status
virtual function can compile, run, but with surprise result
[shangtang@BTSOM-1 study]$ ./a.out
Transaction Constructor
Transaction Log
BuyTransaction Constructor

The only way to avoid this problem is to make sure that none of your constructors or destructors call virtual functions on the object being created or destroyed and that all the functions they call obey the same constraint.

posted @ 2013-05-17 11:27 魏尚堂 閱讀(230) | 評論 (0)編輯 收藏

Depending on the precise conditions under which such pairs of simultaneously active exceptions arise, program execution either terminates or yields undefined behavior. In this example, it yields undefined behavior.
C++ does not like destructors that emit exceptions!
#include <iostream>
#include <vector>
struct Exception
{
    Exception(){std::cout << "Exception Constructor" << std::endl;}
    ~Exception(){std::cout << "Exception Destructor" << std::endl;}
};
class Widget {
public:
  ~Widget() {std::cout << "Widget Destructor" << std::endl; throw Exception();
  }        //this might emit an exception
  void print(){std::cout << "print" << std::endl;}
};
                
void doSomething();
int main()
{
    doSomething();
}
void doSomething()
{
  std::vector<Widget> v;
  v.push_back(Widget());
  v.push_back(Widget());
  v.push_back(Widget());
  v.push_back(Widget());
  std::vector<Widget>::iterator it = v.begin();
  while(it != v.end())
  {
    std::cout << "end" << std::endl;
    (*it).print();
    it++;
  }
}
complie with g++
[shangtang@BTSOM-1 study]$ ./a.out
Widget Destructor
Exception Constructor
terminate called after throwing an instance of 'Exception'
Aborted (core dumped)
There are two primary ways to avoid the trouble.

   1, Terminate the program if catch a exception, typically by calling std::abort (cstdlib)
  2, 
Swallow the exception if catch a exception, print a log

posted @ 2013-05-17 10:56 魏尚堂 閱讀(164) | 評論 (0)編輯 收藏

2013年5月13日

1, Declare destructors virtual in polymorphic base classes
why ? because C++ specifies that when a derived class object is deleted through a pointer to a base class with a non-virtual destructor, results are undefined.What typically happens at runtime is that the derived part of the object is never destroyed

2, if a class is not intended to be a base class, making the destructor virtual is usually a bad idea. 
why?  if a class have virtual functions, it has extra overhead(vptr).

3, In fact, many people summarize the situation this way: declare a virtual destructor in a class if and only if that class contains at least one virtual function

4,Sometimes, however, you have a class that you'd like to be abstract, but you don't have any pure virtual functions.
solution: declare pure virtual destructor.
There is one twist, however you must provide a definition for the pure virtual destructor, or linker will complain.

5, Not all base classes are designed to be used polymorphically. Neither the standard string type, for example, nor the STL container typesare designed to be base classes at all, much less polymorphic ones.


 
 

posted @ 2013-05-13 14:45 魏尚堂 閱讀(238) | 評論 (0)編輯 收藏

2013年1月24日

thinking in c++ 有下面的例子,不太理解為什么這個宏中的條件表達式在編譯時就執行了,在此作個記號

// A simple, compile-time assertion facility
#define STATIC_ASSERT(x) \
   do { typedef int a[(x) ? 1 : -1]; } while(0)

int main()
{
   STATIC_ASSERT(sizeof(int) <= sizeof(long));  // Passes
   STATIC_ASSERT(sizeof(double) <= sizeof(int)); // Fails 
   return 0;
}

posted @ 2013-01-24 10:26 魏尚堂 閱讀(688) | 評論 (0)編輯 收藏

2013年1月21日

template <typename T> class dataList
{
   public:
      friend ostream& operator<<(ostream& outStream, const dataList <T> &outList);
}
template <typename T> ostream& operator<<(ostream& outStream, const dataList <T> &outList)
{
   //....
   return outStream;
}
int main(int argc, char* argv[])
{
   dataList <int> testList;
   cout << testList;
}
這個程序員是鏈接不過,
錯誤信息:
 warning: friend declaration âstd::ostream& operator<<(std::ostream&, const dataList<T>&)â declares a non-template function
 note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
/tmp/cc9DSuka.o: In function `main':
undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, dataList<int> const&)'
collect2: ld returned 1 exit status
錯誤原因解釋

The problem is that the compiler is not trying to use the templated operator<< you provided, but rather a non-templated version.

When you declare a friend inside a class you are injecting the declaration of that function in the enclosing scope. The following code has the effect of declaring (and not defining) a free function that takes a non_template_test argument by constant reference:

class non_template_test { friend void f( non_template_test const & ); }; // declares here: // void f( non_template_test const & );

The same happens with template classes, even if in this case it is a little less intuitive. When you declare (and not define) a friend function within the template class body, you are declaring a free function with that exact arguments. Note that you are declaring a function, not a template function:

template<typename T> class template_test { friend void f( template_test<T> const & t ); }; // for each instantiating type T (int, double...) declares: // void f( template_test<int> const & ); // void f( template_test<double> const & );  int main() {     template_test<int> t1;     template_test<double> t2; }

Those free functions are declared but not defined. The tricky part here is that those free functions are not a template, but regular free functions being declared. When you add the template function into the mix you get:

template<typename T> class template_test { friend void f( template_test<T> const & ); }; // when instantiated with int, implicitly declares: // void f( template_test<int> const & );  template <typename T> void f( template_test<T> const & x ) {} // 1  int main() {    template_test<int> t1;    f( t1 ); }

When the compiler hits the main function it instantiates the template template_test with type intand that declares the free function void f( template_test<int> const & ) that is not templated. When it finds the call f( t1 ) there are two f symbols that match: the non-template f( template_test<int> const & ) declared (and not defined) when template_test was instantiated and the templated version that is both declared and defined at 1. The non-templated version takes precedence and the compiler matches it.

When the linker tries to resolve the non-templated version of f it cannot find the symbol and it thus fails.

What can we do? There are two different solutions. In the first case we make the compiler provide non-templated functions for each instantiating type. In the second case we declare the templated version as a friend. They are subtly different, but in most cases equivalent.

Having the compiler generate the non-templated functions for us:

template <typename T> class test  { friend void f( test<T> const & ) {} }; // implicitly

This has the effect of creating as many non-templated free functions as needed. When the compiler finds the friend declaration within the template test it not only finds the declaration but also the implementation and adds both to the enclosing scope.

Making the templated version a friend

To make the template a friend we must have it already declared and tell the compiler that the friend we want is actually a template and not a non-templated free function:

template <typename T> class test; // forward declare the template class template <typename T> void f( test<T> const& ); // forward declare the template template <typename T> class test { friend void f<>( test<T> const& ); // declare f<T>( test<T> const &) a friend }; template <typename T> void f( test<T> const & ) {}

In this case, prior to declaring f as a template we must forward declare the template. To declare the ftemplate we must first forward declare the test template. The friend declaration is modified to include the angle brackets that identify that the element we are making a friend is actually a template and not a free function.
引用自 http://stackoverflow.com/questions/1810753/overloading-operator-for-a-templated-class
從上面我可以學到一點:
1, 編譯器匹配方法時非模板函數優先模板函數
2, 友元函數模板必須提前聲明

posted @ 2013-01-21 10:48 魏尚堂 閱讀(612) | 評論 (0)編輯 收藏

2013年1月17日

Linux Shell的通配符與正則表達式 http://blog.csdn.net/chen_dx/article/details/2463495

posted @ 2013-01-17 14:01 魏尚堂 閱讀(166) | 評論 (0)編輯 收藏

2012年11月27日

Perform every resource allocation (e.g., new) in its own code statement which immediately gives the new resource to a manager object (e.g., auto_ptr).

This guideline is easy to understand and remember, it neatly avoids all of the exception safety problems in the original problem, and by mandating the use of manager objects it helps to avoid many other exception safety problems as well. This guideline is a good candidate for inclusion in your team's coding standards

link http://www.gotw.ca/gotw/056.htm

file I/O http://www.zwqxin.com/archives/cpp/use-sstream.html

posted @ 2012-11-27 10:03 魏尚堂 閱讀(155) | 評論 (0)編輯 收藏

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久三级视频| 欧美诱惑福利视频| 这里只有精品在线播放| 亚洲高清资源综合久久精品| 狠狠爱成人网| 好吊日精品视频| 一区二区三区在线观看视频| 国产一区二区在线免费观看 | 亚洲日本va午夜在线影院| 乱中年女人伦av一区二区| 欧美一区二区视频在线观看| 一区二区三区高清| 亚洲免费观看高清在线观看| 亚洲精品日韩一| 亚洲美女尤物影院| 日韩亚洲欧美在线观看| 亚洲人成毛片在线播放| 亚洲国产欧美不卡在线观看| 最近中文字幕mv在线一区二区三区四区| 国产一区二区看久久| 国产午夜精品一区二区三区欧美| 欧美激情第六页| 欧美日韩综合视频| 国产精品人人做人人爽 | 久久精品国产91精品亚洲| 亚洲欧美精品suv| 欧美一区二区三区四区在线| 一区二区国产日产| 99精品黄色片免费大全| 亚洲美女在线视频| 伊人久久大香线蕉av超碰演员| 欧美成人tv| 国产欧美一二三区| 国产一区二区三区高清在线观看 | 男女视频一区二区| 麻豆精品传媒视频| 久久久久网址| 国产精品女同互慰在线看| 国产伦一区二区三区色一情| 最新国产拍偷乱拍精品 | 9人人澡人人爽人人精品| 一本一道久久综合狠狠老精东影业 | 亚洲片国产一区一级在线观看| 国产精品99久久久久久人| 亚洲伊人色欲综合网| 欧美大片91| 国产精品免费网站在线观看| 精品盗摄一区二区三区| 欧美一站二站| 亚洲黄色免费电影| 久久色中文字幕| 欧美女同视频| 国产亚洲一区二区三区在线观看| 亚洲激情在线视频| 午夜精品美女自拍福到在线 | 欧美在线一级va免费观看| 老司机午夜精品视频| 亚洲午夜电影| 蜜臀99久久精品久久久久久软件| 欧美国产精品人人做人人爱| 狠狠综合久久av一区二区老牛| 日韩视频一区二区三区在线播放免费观看 | 欧美成人午夜激情视频| 欧美在线免费视频| 欧美人成免费网站| 欧美成人一区在线| 久久精品国产亚洲一区二区三区| 欧美精品一区二区在线播放| 国内精品一区二区三区| 欧美日韩成人网| 国产精品久久久久91| 亚洲宅男天堂在线观看无病毒| 欧美不卡视频| 母乳一区在线观看| 国产一区观看| 99热免费精品在线观看| 亚洲国产一区二区三区a毛片| 久久动漫亚洲| 伊人天天综合| 久久精品亚洲精品| 午夜亚洲福利在线老司机| 欧美日韩欧美一区二区| 亚洲麻豆国产自偷在线| 国产精品毛片在线看| 99re6这里只有精品| 一区二区三区偷拍| 欧美日韩国产综合在线| 亚洲精品你懂的| 日韩亚洲一区二区| 欧美麻豆久久久久久中文| 亚洲一区二区三| 99精品视频免费观看视频| 久久精品72免费观看| 亚洲第一狼人社区| 麻豆freexxxx性91精品| 欧美成人黑人xx视频免费观看| 亚洲国产精品高清久久久| 久久精品免费电影| 你懂的亚洲视频| 99精品热视频| 欧美亚洲系列| 伊人久久大香线蕉av超碰演员| 亚洲精品一区二区在线观看| 欧美特黄一区| 欧美一级淫片播放口| 久久久久久久久久久成人| 亚洲国产精品va在看黑人| 亚洲美女黄色片| 久久精品视频在线看| 乱中年女人伦av一区二区| 亚洲一区三区电影在线观看| 亚洲欧美日本国产专区一区| 亚洲激情黄色| 亚洲精品一线二线三线无人区| 欧美日韩理论| 欧美成人久久| 国产精品白丝av嫩草影院| 美女久久一区| 国产精品久久久久久久久久免费 | 欧美视频网址| 久久久久久伊人| 欧美va亚洲va香蕉在线| 99re8这里有精品热视频免费| 久久久久久穴| 午夜视频在线观看一区二区三区| 亚洲激情成人网| 国产精品天天摸av网| 亚洲日本va午夜在线电影| 国产亚洲亚洲| 亚洲精品亚洲人成人网| 亚洲日本va午夜在线影院| 一本久久a久久免费精品不卡| 亚洲精品美女在线观看播放| 亚洲一区欧美二区| 亚洲激情精品| 美女视频黄a大片欧美| 亚洲小视频在线| 欧美日本免费| 久久亚洲综合网| 韩日在线一区| 亚洲砖区区免费| 在线亚洲高清视频| 欧美日韩天堂| 亚洲国产小视频| 亚洲欧洲精品一区二区三区 | 欧美专区在线观看一区| 欧美一区激情视频在线观看| 欧美激情一区三区| 欧美大片免费观看在线观看网站推荐 | 午夜亚洲视频| 欧美日韩精品二区| 日韩一级片网址| 日韩亚洲视频| 欧美三级午夜理伦三级中文幕| 亚洲国产99| 亚洲手机成人高清视频| 欧美激情91| 久久免费视频观看| 经典三级久久| 久久精品免费播放| 欧美激情按摩在线| **欧美日韩vr在线| 午夜在线视频观看日韩17c| 亚洲精品视频免费| 久久偷窥视频| 日韩视频一区二区三区| 日韩写真视频在线观看| 欧美视频在线一区二区三区| 久久天堂成人| 99国产精品99久久久久久粉嫩| 欧美电影免费| 一区二区三区国产精品| 亚洲性感美女99在线| 国产欧美在线视频| 欧美一区二区三区精品电影| 久久爱www.| 亚洲国产高清视频| 欧美成人亚洲成人| 在线视频日韩| 久久视频一区| 亚洲一区二区动漫| 国产精品高清在线| 99国产精品私拍| 久久蜜臀精品av| 亚洲成在线观看| 国产精品99久久久久久有的能看| 欧美资源在线观看| 日韩午夜精品| 国产精品一区二区在线观看网站| 久久综合一区二区三区| 亚洲美女91| 亚洲尤物影院| 欧美国产一区视频在线观看| 久久亚洲电影| 亚洲综合日韩在线| 精品成人一区| 国产欧美成人| 欧美成人午夜激情视频| 久久不射中文字幕| 亚洲三级毛片|