随笔分类 - cplusplus
摘要:http://c-faq.com/ansi/constasconst.html
阅读全文
posted @ 2013-09-22 17:58
strorehouse
摘要:在有虚函数成员的情况下,构造函数和析构函数都会有不透明的对虚函数指针的写操作。 因此,在构造函数和析构函数中调用虚函数是危险的行为。
阅读全文
posted @ 2013-09-12 20:44
strorehouse
摘要:c++的模板还是没学好啊,Curiously recurring template pattern是c++中的一个常见习语。其主要作用就是实现静态多态。
阅读全文
posted @ 2013-09-09 20:12
strorehouse
摘要:按BS大叔的说法,c和c++的const之间有着微妙联系。这里有个不错的解释:Overloading on const is wrong
阅读全文
posted @ 2013-09-09 19:30
strorehouse
摘要:之前虽然读过部分sgi-stl,可从没注意到这点,解释在此。
阅读全文
posted @ 2013-09-07 22:10
strorehouse
摘要:sizeof('a')在c和c++中的返回值是不一样的,半年前做总结时发现的。今天才知道,c++ faq里其实有提到的,只是我没看到。
阅读全文
posted @ 2013-09-07 15:44
strorehouse
摘要:在C++0X之前,似乎只能用函数指针来做,但不太自然。但现在情况不一样了,废话少话说,直接给链接吧:
阅读全文
posted @ 2013-08-06 19:41
strorehouse
摘要:昨天看到有人在说这个,确实比较有意思。既不违反语法,又比较出乎意料。就像C里面那种switch里嵌套一层do while语句一样。void cpy(char* to, char* from, size_t count){ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: ...
阅读全文
posted @ 2013-05-02 09:21
strorehouse
摘要:某些人会考虑用long类型的长度来作为判断的标准,这样不是一个好的答案。我认为比较好的答案是利用虚拟地址的位数:1 int main()2 {3 4 #ifdef __x86_64__5 assert(sizeof(char *) == 8);6 #endif7 return 0;8 }然后见到别人提了一个这样的问题:如果是64位的机器装了32位的系统,你怎么判断呢?A:1. 直接读取/proc/cpuinfo2.更底层点的做法:使用汇编指令cpuid
阅读全文
posted @ 2013-04-24 09:46
strorehouse
摘要:REFER:SOWhat are these new categories of expressions?The FCD (n3092) has an excellent description:— An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [ Example: If E is an expression of pointer type
阅读全文
posted @ 2013-04-08 19:52
strorehouse
摘要:REFER:SOAs of theC++11standard, string-to-number conversion and vice-versa are built in into the standard library. All the following functions are present in<string>(as per paragraph 21.5).string to numericfloat stof(const string& str, size_t *idx = 0);double stod(const string& str, si
阅读全文
posted @ 2013-04-08 19:48
strorehouse
摘要:REFER:SO中文社区见到的答案,不提也罢new/deleteAllocate/release memoryMemory allocated from 'Free Store'Returns a fully typed pointer.new (standard version) never returns a NULL (will throw on failure)Are called with Type-ID (compiler calculates the size)Has a version explicitly to handle arrays.Reallocati
阅读全文
posted @ 2013-04-08 19:33
strorehouse
摘要:C++ mechanisms for polymorphismExplicit programmer-specified polymorphismYou can writef()such that it can operate on multiple types in any of the following ways:Preprocessing:#define f(X)((X)+=2)// (note: in real code, use a longer uppercase name for a macro!)Overloading:void f(int& x){ x +=2;}v
阅读全文
posted @ 2013-04-04 20:10
strorehouse
摘要:网上某些流传的答案是错误的,以讹传讹,呵呵In c++ 03:default ctorcopy ctorassignment operatordefault destructorIn c++ 0x:default ctorcopy ctorassignment operatordefault destructormove constructormove-assignment operator
阅读全文
posted @ 2013-04-02 20:15
strorehouse
摘要:View Code /*refer:http://stackoverflow.com/questions/270947/can-any-one-provide-me-a-sample-of-singleton-in-c/271104#271104 */#include <vector>#include <string>#include <iostream>using namespace std;class Sin{ public: static Sin& getInstance() { static Sin instance; ...
阅读全文
posted @ 2013-04-01 21:21
strorehouse
摘要:去年就在某牛人的博客上见到了,现在准备去玩玩。做题的方式跟Topcoder类似,刚刚去水了一题"Valid Palindrome" 1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 string alnumstr; 7 transform(s.begin(), s.end(), s....
阅读全文
posted @ 2013-03-08 18:26
strorehouse
摘要:1 /* 2 simple memory manage demo from TCPL 3 should be compiled in unix 4 debug to see how it works 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 11 typedef long Align; 12 13 union header 14 { 15 struct 16 { 17 union header* p...
阅读全文
posted @ 2012-10-29 14:27
strorehouse
浙公网安备 33010602011771号