这两个不同的语句都创建一个包含6个数字的新数组:
var points = new Array(40, 100, 1, 5, 25, 10) // Bad
var points =[40, 100, 1, 5, 25, 10]; // Good
Run Code Online (Sandbox Code Playgroud)
但是没有解释为什么第一个是坏的.
据我所知,唯一的区别是第一个调用构造函数.有人能告诉我为什么第一个是坏的?
我有一个场景,我已经创建了一个类,调用它myClass,我意识到我需要另一个类,调用它myOtherClass,它将在内部使用,myClass但不在其中.我多年没有上过计算机科学课,所以我不记得我想要的术语.没有继承; 只是它myClass使用myOtherClass,实际上构建了一个myOtherClass对象树,我想要正确地封装所有东西.
我需要遵循的概念是什么,具体是什么?如果我需要尝试让这个问题更清楚,请告诉我.
我在我的代码一个地方,我有一个元素iter类型的std::string::const_iterator,如果*iter是一个小数的开始(即形式的字符串的开始-00.01,.60,12319.1,99,等),那么我想推进iter,直到它通过传递十进制字符串,我想存储十进制字符串.
对于场景的具体设置:
for (std::string::const_iterator iter(str.begin()), offend(str.end()); iter != offend; ++iter)
{
if (iter == '(')
{
//... blah blah blah
}
else if (iter is the beginning of a decimal representation)
{
parse the decimal representation, store it in a string, advance iter to the character at the end of the decimal representation (or to offend -- whichever comes first)
}
else if (some …Run Code Online (Sandbox Code Playgroud)