我正在阅读Scott Meyers的书,并看到以下示例:
class Rational { ... };
const Rational operator*(const Rational& lhs, const Rational& rhs);
Rational a, b, c;
...
(a * b) = c; // invoke operator= on the
// result of a*b!
Run Code Online (Sandbox Code Playgroud)
他说这真的很奇怪,但我不明白为什么.operator=
对结果的调用有什么问题a*b
?
这个问题可能听起来很傻,但为什么我们不能这样做呢?我的意思是,声明者如下:
void (foo())();
Run Code Online (Sandbox Code Playgroud)
我已经阅读了当前C++标准的8.3.5部分,并没有从它所说的内容中找到它的含义.
这是标准所说的:
在TD的声明中,D表格
D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
ref-qualifieropt exception-specificationopt attribute-specifier-seqopt
Run Code Online (Sandbox Code Playgroud)
并且声明T D1中包含的declarator-id的类型是"derived-declarator-type-list T",D中的declarator-id的类型是"parameter-declarator-type-list function of(parameter-declaration) -clause)cv-qualifierseqopt ref-qualifieropt返回T".
因此,正式地说,从该定义暗示我的declration是一个有效的函数declration.T D1
在我的情况下,形式void foo()
是一个完全有效的declration.我错过了什么?
我是一名 Java 程序员,对提供纯虚拟函数定义的事实感到困惑。在 Java 中,我习惯于将抽象方法视为我们将在基类中提供定义的方法。但是下面的代码是完全有效的:
#include <iostream>
struct A
{
virtual ~A() = 0;
};
A::~A(){ std::cout << "~A()" << std::endl; }
struct B : A
{
~B(){ std::cout << "~B()" << std::endl; }
};
A *a = new B;
int main()
{
delete a;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我们尝试做这样的事情:
#include <iostream>
struct A
{
virtual ~A() = 0;
virtual void foo() = 0;
};
void A::foo(){ }
A::~A(){ std::cout << "~A()" << std::endl; }
struct B : A
{
~B(){ std::cout << …
Run Code Online (Sandbox Code Playgroud) 我写了以下简单的例子:
#include<iostream>
#include<vector>
int main()
{
int arr[] = {1, 2, 4, 7, 10};
std::vector<int> vect;
vect.assign(arr, arr + 5);
for(std::vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
{
std::cout << *it << std::endl;
}
std::cout << "-------------------------------------" << std::endl;
for(std::vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
{
std::cout << *it << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
并且两个循环打印相同。我的问题是它可靠吗?迭代向量是否每次都以相同的顺序返回元素?我的意思是,它是标准化的还是允许某些实现以不同的顺序迭代向量。例如,我们第一次迭代向量如下:
for(std::vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
{
std::cout << *it << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
并获得输出
1
2
4
7
10
Run Code Online (Sandbox Code Playgroud)
同时,第二次迭代产生输出: …
我以为指针指向NULL
可以转换为0
.所以我写了以下程序:
#include <iostream>
#include <regex>
#include <string>
#include <cstdlib>
bool is_strtoi(const std::string& str)
{
char *p;
std::strtol(str.c_str(), &p, 10);
return *p == '\0';
}
bool is_strtoi1(const std::string& str)
{
char *p;
std::strtol(str.c_str(), &p, 10);
return p; //Pointer to NULL should be converted to false;
}
int main ()
{
std::string test1("123asd2");
std::string test2("123123");
std::cout << is_strtoi1(test1) << std::endl;
std::cout << is_strtoi1(test2) << std::endl;
std::cout << "---" << std::endl;
std::cout << is_strtoi(test1) << std::endl; //prints 1
std::cout …
Run Code Online (Sandbox Code Playgroud) 我试图从std::exception
这样得出:
class bad_number : public std::exception
{
public:
bad_number(const char*);
virtual const char* what() const noexcept;
private:
const char* num;
};
bad_number::bad_number(const char * num) : num(num){ }
const char* bad_number::what() const noexcept
{
std::string str;
str.append("Invalid number format:");
str.append(num);
char* result = new char[str.size()];
strcpy(result, str.c_str());
return result;
}
//The function that uses the exception
long double convert(const char *str)
{
char *endptr;
double result = strtold(str, &endptr);
if (*endptr != '\0')
throw bad_number(str);
return result;
} …
Run Code Online (Sandbox Code Playgroud) Mojarra 2.1.
我检查public void execute(FacesContext facesContext) throws FacesException
了班上的方法com.sun.faces.lifecycle.RestoreViewPhase
.现在,一旦视图通过invokation恢复, viewRoot = viewHandler.restoreView(facesContext, viewId);
我们就可以获得前一个请求中的竞争树(如果它是回发).
我检查了那棵树(手动迭代children
调试器中组件的属性)并发现复合组件,声明如下:
<h:panelGroup rendered="#{bean.id == 1}">
<utils:dropDownListFilterItem />
</h:panelGroup>
<h:panelGroup rendered="#{bean.id == 2}">
<utils:dateFilterItem />
</h:panelGroup>
<h:panelGroup rendered="#{bean.id == 3}">
<utils:fieldFilterItem />
</h:panelGroup>
Run Code Online (Sandbox Code Playgroud)
都在那棵树里.尽管#{bean.id}
被评估为2 ,但它很高兴.我发现唯一的<utils:dateFilterItem />
实例将在树中.
所以,我的问题是rendered
Mojarra 如何处理属性?属性是否会影响唯一的渲染响应阶段?
我有以下课程:
public class Service{
private static final ExecutorService executor = Executors.newFixedThreadPool(4);
public synchronized void execute(Collection<Runnable> runs){
for(Runnable r: runs){
executor.execute(r);
}
}
public boolean isComplete(){
//should return true iff all tasks applied by the execute(Collection<Runnable> runs)
//method're finished their execution (normally or abruptly,
//it doesn matter)
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何实现该isComplete()
方法。我需要检查当前是否有正在进行的任务。如果执行器被清除(所有任务都完成),则该方法应返回 true,否则返回 false。
问题可能看起来很明显,但无论如何我都不清楚.
我有以下课程:
public class MyClass{
private Object lock = new Object();
private boolean flag;
public void method(){
//Start synchronization block
if(!flag){
flag = true;
//end synchronization block
//do some bulk operation, should be synchronized
}
//some other staff
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我不能把我需要同步的代码片段,因为它不正确.怎么做这样的同步?也许有一些java.util.concurrent
我可以利用的东西.
我正在阅读J. Bloch的"Effective Java",现在我正处于关于for-each
vs 的部分for-loop
.他提到了三种我们无法使用for-each
循环的情况,其中一种情况如下:L
并行迭代 - 如果需要并行遍历多个集合,则需要对迭代器或索引变量进行显式控制,以便所有迭代器或索引变量都可以锁步前进(如上面的有缺陷的卡和骰子示例中无意中所示) .
这个案子对我来说不太清楚,我无法想象一个例子.
突然出现在我脑海中的第一个想法是它只是在多个线程中迭代相同的集合,但它可能不是他的意思.我没有看到任何限制阻止我们这样做(只读).其实:
public class MyRunnable implements Runnable{
private Collection<String> col;
//CTOR ommmited
public void run(){
for(String s : col){
//print o, not modify
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们用相同的实例启动一些线程.因此,我们不害怕获取ConcurrentModificationException
(JavaDocs)因为我们执行只读访问,即使是同时多个线程也是如此.
怎么了?