#include <cstdio>
#include <string>
void fun(const char* c)
{
printf("--> %s\n", c);
}
std::string get()
{
std::string str = "Hello World";
return str;
}
int main()
{
const char *cc = get().c_str();
// cc is not valid at this point. As it is pointing to
// temporary string internal buffer, and the temporary string
// has already been destroyed at this point.
fun(cc);
// But I am surprise this call will yield valid result.
// It seems that the returned …Run Code Online (Sandbox Code Playgroud) Joshua Bloch给出的建议之一是,类应该被设计为不可变的.
我有以下课程
public class Dividend {
public Dividend setDate(SimpleDate date) {
Dividend dividend = new Dividend(this.getStock(), this.getAmount(), date);
return dividend;
}
.....// More to go.
Run Code Online (Sandbox Code Playgroud)
对于setDate方法,不会修改此对象.
相反,一个克隆副本,这与它的日期字段被修改将被退回.
但是,通过从方法名称判断,用户将如何知道此对象仍将保持不变?
除了setDate之外还有更好的命名约定吗?
我想知道,在下面的情况下我需要调用DeleteObject吗?
CFont* oldFont = label.GetFont();
LOGFONT oldLogFont;
oldFont->GetLogFont(&oldLogFont);
oldLogFont.lfWeight = FW_BOLD;
CFont newFont;
newFont.CreateFontIndirectW(&oldLogFont);
label.SetFont(&newFont, true);
// Do I need to call oldFont->DeleteObject() or newFont->DeleteObject()?
Run Code Online (Sandbox Code Playgroud)
谢谢。
好.我们知道以下代码无法编译.
char source[1024];
char dest[1024];
// Fail. Use memcpy(dest, source, sizeof(source)); instead.
dest = source;
Run Code Online (Sandbox Code Playgroud)
但是,以下代码可以编译并且行为正确.
class A {
char data[1024];
};
A source;
B dest;
dest = source;
Run Code Online (Sandbox Code Playgroud)
我想知道,在运算符赋值函数中,数组将是隐式的memcpy吗?
以下是完整的测试代码.
#include <cstdio>
#include <memory>
class A {
public:
char data[1024];
};
int main() {
{
A source;
A dest;
// Initialization
char *data = "hello world";
memcpy (source.data, data, strlen(data) + 1);
printf ("source.data = %s\n", source.data);
printf ("address source.data = %x\n", source.data);
// Works! Does this in the …Run Code Online (Sandbox Code Playgroud) 我来自CVS背景.
目前,我有2个并行开发的mercurial存储库.hello-world-bugfix和hello-world-feature(这个是从克隆hello-world-bugfix)
现在,我想将错误的固定代码合并hello-world-bugfix到一起hello-world-feature,以便在一天结束时,我将得到一个合并文件.
[BUG2 BUG2 BUG2]
START
[BUG1 BUG1 BUG1]
[FEATURE2 FEATURE2 FEATURE2]
Run Code Online (Sandbox Code Playgroud)
以下是所有事情之前的样子,拉动和合并.以下两个文件已经提交.


现在,我执行拉动hello-world-feature,从中拉出变化hello-world-bugfix.

然后,我执行更新,always merge打开选项.

这是我的合并文件???
[BUG2 BUG2 BUG2]
START
[BUG1 BUG1 BUG1]
Run Code Online (Sandbox Code Playgroud)
似乎我之前的承诺[FEATURE2 FEATURE2 FEATURE2]被覆盖了.
看来,我不应该进行更新步骤,这将不合并feature有bug,但覆盖feature逃脱bug.拉后应该做的下一个正确步骤是什么?(通过TortoiseHg),以便我可以获得错误修复代码,并保留功能代码吗?
这是最终的存储库视图 hello-world-feature

请考虑以下代码.为了防止IndexOutOfBoundsException在调用时listIterator,我们使用读取器锁来检索基于索引的iteartor,并在写入操作时使用写入器锁定其他位置stockCodes.
请注意,我们没有使用任何锁定机制来迭代使用listIterator,因为它来自CopyOnWriteArrayList.不需要锁定,因为ConcurrentModificationException不应该抛出.
// stockCodesReaderLock is reader lock from java.util.concurrent.locks.ReadWriteLock
// stockCodes is CopyOnWriteArrayList
// Acquire iterator in a safe way.
stockCodesReaderLock.lock();
final int stockCodesSize = stockCodes.size();
if (currIndex < stockCodesSize) {
listIterator = stockCodes.listIterator(currIndex);
}
stockCodesReaderLock.unlock();
Run Code Online (Sandbox Code Playgroud)
我想知道,我是否应该try/finally阻止,因为我看不出有任何异常出现的机会?如果try/finally必须使用,我应该使用(A)还是(B)?
我有什么需要吗?
(一个)
try {
stockCodesReaderLock.lock();
final int stockCodesSize = stockCodes.size();
if (currIndex < stockCodesSize) {
listIterator = stockCodes.listIterator(currIndex);
}
} finally {
stockCodesReaderLock.unlock(); …Run Code Online (Sandbox Code Playgroud) 最近,我遇到了这个one stone kills multiple birds框架 - http://phonegap.com/.
我确实在尝试访问手机的原生功能时遇到了一些限制 - http://www.phonegap.com/features.但是在当前这一点对我来说并不重要,因为我现在不需要这些原生功能.(2D瓷砖游戏)
我想知道,你们中的任何人都使用上述框架开发了任何应用程序?根据以下情况,您发现的差异是什么:
我意识到这ofstream不适用于Windows 7隐藏文件.
这是快速测试代码.
#include <fstream>
#include <iostream>
#include <tchar.h>
#include <windows.h>
int main() {
{
std::ifstream file2(_T("c:\\a.txt"));
if (file2.is_open()) {
std::cout << "ifstream open" << std::endl;
} else {
std::cout << "ifstream not open!" << std::endl;
}
}
// SetFileAttributes(_T("c:\\a.txt"), FILE_ATTRIBUTE_NORMAL);
SetFileAttributes(_T("c:\\a.txt"), FILE_ATTRIBUTE_HIDDEN);
{
std::ofstream file(_T("c:\\a.txt"));
if (file.is_open()) {
std::cout << "ofstream open" << std::endl;
} else {
std::cout << "ofstream not open!" << std::endl;
}
}
getchar();
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出
ifstream open
ofstream not open!
Run Code Online (Sandbox Code Playgroud)
如果我使用FILE_ATTRIBUTE_NORMAL, …
我习惯于Joshua Bloch的Effective Java给出的建议Item 52: Refer to objects by their interfaces.
但是,在Android的大多数示例代码中,我意识到以下代码非常常见.
private ArrayList<Integer> mPhotos = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
我理解这是由于性能优化的目的,因为下面的代码会更慢.
private List<Integer> mPhotos = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
但是,这种优化技术仍然有效吗?好像我从http://developer.android.com/guide/practices/design/performance.html上读到
在没有JIT的设备上,通过具有精确类型而不是接口的变量调用方法确实更有效.(例如,调用HashMap映射上的方法比使用Map映射更便宜,即使在这两种情况下映射都是HashMap.)情况并非如此慢2倍; 实际差异更像是慢了6%.此外,JIT使两者有效地难以区分.
我们是否需要假设我们的设备没有JIT,并且引用没有接口的对象?或者,我们应该接受Joshua Bloch的建议吗?
a.py
#!c:/Python27/python.exe -u
from connection import Connection
import globals
globals.server_ip = '192.168.0.1'
connection = Connection()
Run Code Online (Sandbox Code Playgroud)
globals.py
#!c:/Python27/python.exe -u
server_ip = '127.0.0.1'
Run Code Online (Sandbox Code Playgroud)
connection.py
import globals
class Connection:
def __init__(self, server_ip = globals.server_ip):
print 'Connection is ' + server_ip + '\n'
Run Code Online (Sandbox Code Playgroud)
我原以为我会Connection is 192.168.0.1被打印出来.但是,相反,Connection is 127.0.0.1正在印刷.
除非我尝试通过显式传递参数来构造连接(这不是我想要的,因为我不愿意在连接0参数上进行更改)
connection = Connection(globals.server_ip)
为什么会这样?我还能申请其他技术吗?