在这种情况下,静态变量应该只有一个或零个实例.这取决于是否f()被召唤.
void f()
{
static int a;
}
Run Code Online (Sandbox Code Playgroud)
但是如果f()是一个方法,那么静态变量有多少个实例呢?
class A
{
void f()
{
static int a;
}
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用JSON和Qt JSON类.例如,我想创建一个简单的QJsonDocument,将其保存到文件,将其加载到另一个文件QJsonDocument并比较结果.
我成功创造了一个QJsonDocument.但是,QJsonDocument 界面中没有简单的命令将其保存到文件中.这同样适用于加载该文件中的文件.
#include <QJsonObject>
#include <QJsonDocument>
#include <QVariant>
int main()
{
QVariantMap map;
map.insert("integer", 1);
map.insert("double", 2.34);
map.insert("bool", QVariant(true));
map.insert("string", "word");
QJsonObject object = QJsonObject::fromVariantMap(map);
QJsonDocument document;
document.setObject(object);
// ?? save document to file
// ?? load file to document
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个答案显示了如何加载文档
QFileQFile为QStringQString为QByteArrayQJsonDocument从中构建QByteArray有没有更简单的方法来做到这一点?
考虑一下问题:
我们有一个Base抽象方法的类.现在,我们希望强制执行此方法的每个覆盖都将执行一些参数检查或其他一些苦差事.我们希望这个参数检查在所有覆盖中都是相同的.一种解决方案是将此行为包装在一个非抽象方法中,该方法调用一个抽象方法:
abstract class Base
{
fun computeSomething(argument: Int): Int
{
require(argument > 0) // Some intricate checking
return execute(argument)
}
// Pure functionality, assuming correct arguments
// Ideally, this would be private.
protected abstract fun execute(validArgument: Int): Int
}
class Derived1: Base()
{
override fun execute(validArgument: Int): Int =
validArgument * validArgument
}
class Derived2: Base()
{
override fun execute(validArgument: Int): Int =
validArgument * validArgument * validArgument
}
fun main(args: Array<String>)
{
val d = Derived1() …Run Code Online (Sandbox Code Playgroud) 我通过向spring-boot-starter-data-mongodb-reactiveSpring Boot 项目添加依赖项来使用反应式 MongoDb 驱动程序。升级到 Spring Boot 2.2.x 后出现此错误。
事实证明,如果我做一些简单的事情:
class Something(@Id val name: String)
@Repository
interface SomethingRepository: ReactiveCrudRepository<Something, String>
@SpringBootTest
class DemoApplicationTests
{
@Autowired protected lateinit var repository: SomethingRepository
@Test
fun test()
{
repository
.save( Something("1") )
.onErrorContinue { throwable, _ -> println(throwable.message) }
.block()
}
}
Run Code Online (Sandbox Code Playgroud)
我得到输出:
...
2019-12-12 20:58:48.379 INFO 24425 --- [ Test worker] com.example.demo.DemoApplicationTests : Started DemoApplicationTests in 2.545 seconds (JVM running for 3.987)
No transaction in context
No transaction in context
... …Run Code Online (Sandbox Code Playgroud) 我很惊讶地看到这个程序甚至可以编译,但结果更让我惊讶:
import java.util.Collections.swap
fun main(args: Array<String>)
{
val immutableList = List(2) { it } // contents are [0, 1]
swap(immutableList, 0, 1)
println(immutableList) // prints [1, 0]
}
Run Code Online (Sandbox Code Playgroud)
该swap函数在库中实现为:
public static void swap(List<?> list, int i, int j) {
list.set(i, list.set(j, list.get(i)));
}
Run Code Online (Sandbox Code Playgroud)
其中List是可变的 Java 列表,而不是不可变的 Kotlin 列表。所以我认为其他 Java 函数也能工作。例如:
reverse(immutableList)
Run Code Online (Sandbox Code Playgroud)
工作,但其他人,如fill函数,甚至不编译:
fill(immutableList, 3)
Run Code Online (Sandbox Code Playgroud)
产生以下错误消息:
类型推断失败:fun fill(p0: MutableList!, p1: T!): Unit cannot be applied to (List,Int) Type mismatch: inferred type is List but MutableList! …
class Widget;
std::vector< std::shared_ptr<Widget> > container
class Criterium
{
public:
bool operator()(const Widget& left, const Widget& right)const;
};
Run Code Online (Sandbox Code Playgroud)
如何根据Criterium对容器进行排序,而不是定义另一个标准,如:
class CriteriumForPointers
{
public:
bool operator()(const std::shared_ptr<Widget>& left,
const std::shared_ptr<Widget>& right)const;
};
Run Code Online (Sandbox Code Playgroud) 在我使用的一个库中,我看到了这段代码:
template<typename T>
void f(SomeTemplatedClass<T> input)
{
(void)input;
...
use(input); // code that uses input
}
Run Code Online (Sandbox Code Playgroud)
我不知道这段代码的含义是什么.如果我将演员阵容移除为无效,我会得到一个
statement has no effect
Run Code Online (Sandbox Code Playgroud)
gcc中的警告.所以我想有人故意这样做,故意添加演员以摆脱警告.
您是否对任何无效的陈述有任何经验,但由于某种原因需要它?
编辑:
假设这与模板无关,这是安全的吗?例如绕过旧的编译器错误等?
当我执行以下操作时:
template <typename T>
class Container
{
public:
class Iterator
{
friend bool operator==(const Iterator& x, const Iterator& y);
};
};
Run Code Online (Sandbox Code Playgroud)
gcc给了我以下警告和建议:
warning: friend declaration
'bool operator==(const Container<T>::Iterator&,
const Container<T>::Iterator&)'
declares a non-template function [-Wnon-template-friend]
friend bool operator==(const Iterator& x, const Iterator& y);
^
(if this is not what you intended,
make sure the function template has already been declared
and add <> after the function name here)
Run Code Online (Sandbox Code Playgroud)
我很确定这是一个新的警告,因为我总是这样做,从来没有任何问题.
有人可以解释为什么这是一个警告,它警告什么?
根据包括Wikipedia在内的一些资料,实现二叉树的两种最常用的方法是:
第二个显然在内存使用和引用的位置方面优越。但是,如果您希望以某种可能使树不平衡的方式从树中插入和移除,可能会导致问题。这是因为此设计的内存使用量是树深度的指数函数。
假设您要支持此类插入和删除。如何实现树,以便树遍历可以充分利用CPU缓存。
我正在考虑为节点创建对象池并将它们分配到数组中。这样,节点将彼此靠近->因此具有良好的参考位置。
但是,如果节点的大小与缓存行的大小相同,这有意义吗?
如果您的L1行大小为64字节,并且访问的第一个成员std::vector<std::uint8_t>(64),则可能会在L1缓存中包含向量的全部内容。这意味着您可以非常快速地访问任何元素。但是,如果元素的大小与缓存行的大小相同怎么办?由于L1,L2和L3高速缓存的高速缓存行可能不会有很大差异,因此,似乎没有办法在此提供参考位置的帮助。我错了吗?还有什么可以做的?
我正在尝试在 Ubuntu 16.04 上使用Spring Tool Suite 3.8.3。启动后我收到此错误:
期间发生内部错误:“初始化 Java 工具”
详细消息:
在“初始化 Java 工具”期间发生内部错误。无法找到用于堆栈图生成的 Asm(正在查找“aj.org.objectweb.asm.ClassReader”)。需要生成编织代码的堆栈图,以避免在 Java 1.7 或更高运行时上编织类型 org.eclipse.jdt.core.search.SearchPattern 时在编织类时出现验证错误
我不得不承认,我不知道我应该在这里做什么,而且我在网上找不到任何指示。欢迎任何建议或提示。
c++ ×6
java ×2
kotlin ×2
templates ×2
binary-tree ×1
c++11 ×1
collections ×1
cpu-cache ×1
eclipse ×1
installation ×1
interop ×1
json ×1
mongodb ×1
qt ×1
qt5 ×1
spring-boot ×1
static ×1
ubuntu ×1
warnings ×1