当我们序列化对象时,静态成员不是序列化的,但是如果我们需要这样做,有什么办法吗?
有没有办法使用ECMAScript6 class
表示法来声明静态类变量或实例变量的默认值?如果没有class
我的想法就会被写成
function MyClass(arg) { if(arg) this.arg = arg; }
MyClass.classVariable = 42;
MyClass.prototype.arg = "no arg specified";
Run Code Online (Sandbox Code Playgroud)
在我看来,最明显的类似ES6的符号本来就是
class MyClass {
constructor(arg) { if(arg) this.arg = arg; }
static let classVariable = 42;
let arg = "no arg specified";
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为根据当前的规范草案,ClassElement的唯一产品是静态的,实例方法和分号都是它们自己.好吧,可以使用一对getter和setter方法来实现与我概述的类似语义,但我认为这会严重影响性能并且语法非常奇怪.
是否有一些草案建议在class
表示法中包含变量,不管怎样?如果是这样,建议的语法是什么,它在哪里发布,在哪里讨论,讨论是如何进行的,以及当前的事态是什么?目前的情况是,如果之前没有讨论过这样的问题,在任何层面都无法回答这个问题,但我认为这不太可能.
一点背景:我目前正在使用谷歌闭包编译器执行高级编译,使用ES6作为输入.为了实现这一点,我需要一个地方来为成员变量添加类型注释,并且我曾经使用语法来放置它们,就像/** @type {string} */ MyClass.prototype.arg;
ECMAScript中的语义无操作一样,但是为闭包编译器提供类型信息很简单.我还没有找到一个用class
构造做同样好的方法.但如果你想解决这个问题,那就是评论.上面的问题是关于成员声明,而不是无操作,所以这里应该讨论的答案.
我想要一个constexpr
从constexpr
函数计算的值(即编译时常量).我想要将这两个作用于类的命名空间,即静态方法和类的静态成员.
我第一次写这个(对我来说)明显的方式:
class C1 {
constexpr static int foo(int x) { return x + 1; }
constexpr static int bar = foo(sizeof(int));
};
Run Code Online (Sandbox Code Playgroud)
g++-4.5.3 -std=gnu++0x
对此说:
error: ‘static int C1::foo(int)’ cannot appear in a constant-expression
error: a function call cannot appear in a constant-expression
Run Code Online (Sandbox Code Playgroud)
g++-4.6.3 -std=gnu++0x
抱怨:
error: field initializer is not constant
Run Code Online (Sandbox Code Playgroud)
好吧,我想,也许我必须把事情从课堂上移开.所以我尝试了以下方法:
class C2 {
constexpr static int foo(int x) { return x + 1; }
constexpr static int bar;
}; …
Run Code Online (Sandbox Code Playgroud) 给出以下代码:
#include <iostream>
template <std::size_t N>
struct foo
{ static std::size_t value; };
template <>
std::size_t foo<0>::value = 0u;
template <size_t N>
std::size_t foo<N>::value = 1u + foo<N - 1u>::value;
int main()
{
std::cout
<< foo<3u>::value << ' '
<< foo<2u>::value << ' '
<< foo<1u>::value << ' '
<< foo<0u>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在递归地初始化value
模板结构的静态成员的地方foo
,我从g ++得到了不同的输出:
3 2 1 0
Run Code Online (Sandbox Code Playgroud)
和从c ++++:
1 1 1 0
Run Code Online (Sandbox Code Playgroud)
看来g ++ foo<N>::value
使用foo<N-1u>::value
clang ++对0使用零的初始化值来递归初始化foo<N-1u>::value
。
两个问题: …
C++中的模板类可以有静态成员吗?由于它不存在并且在使用之前是不完整的,这可能吗?
到目前为止,我一直在使用Session将一些变量从一个页面传递到另一个页面.例如用户角色.当用户登录到Web应用程序时,用户的角色ID保留在Session中,并且在应用程序的不同部分检查该角色.我最近开始考虑为什么不使用静态成员.我可以将相同的信息存储在静态字段中,并可以在我的应用程序的任何地方轻松访问它(包括静态字段所在的命名空间的任何位置.)我知道使用Session变量有时会派上用场,这样:
除了上述之外,还有其他原因我不应该使用静态字段来存储数据并让它随处可用吗?
static
在C++中初始化数据成员的正确方法是什么?我也对它从C++ 98,C++ 11到C++ 14的变化感兴趣.
这是一个例子:
// bufferedOutput.h
class BufferedOutput
{
// Static member declaration.
static long bytecount;
};
// bufferedOutput.cpp
long BufferedOutput::bytecount = 50;
Run Code Online (Sandbox Code Playgroud)
还有其他方法来初始化static
数据成员吗?
class base {
public:
base a;
};
Run Code Online (Sandbox Code Playgroud)
它给出了编译错误.
class base {
public:
static base a;
};
Run Code Online (Sandbox Code Playgroud)
而这段代码不会给出编译错误
我正在尝试创建一个通用函数,从std :: vector中删除重复项.由于我不想为每个矢量类型创建一个函数,我想让它成为一个可以接受任何类型矢量的模板函数.这是我有的:
//foo.h
Class Foo {
template<typename T>
static void RemoveVectorDuplicates(std::vector<T>& vectorToUpdate);
};
//foo.cpp
template<typename T>
void Foo::RemoveVectorDuplicates(std::vector<T>& vectorToUpdate) {
for(typename T::iterator sourceIter = vectorToUpdate.begin(); (sourceIter != vectorToUpdate.end() - 1); sourceIter++) {
for(typename T::iterator compareIter = (vectorToUpdate.begin() + 1); compareIter != vectorToUpdate.end(); compareIter++) {
if(sourceIter == compareIter) {
vectorToUpdate.erase(compareIter);
}
}
}
}
//SomeOtherClass.cpp
#include "foo.h"
...
void SomeOtherClass::SomeFunction(void) {
std::vector<int> myVector;
//fill vector with values
Foo::RemoveVectorDuplicates(myVector);
}
Run Code Online (Sandbox Code Playgroud)
我一直收到链接器错误,但它编译得很好.关于我做错了什么的任何想法?
更新:基于Iraimbilanja给出的答案,我去重写了代码.但是,万一有人想要使用代码来执行RemoveDuplicates函数,这里是:
//foo.h
Class Foo {
template<typename T>
static void RemoveVectorDuplicates(T& …
Run Code Online (Sandbox Code Playgroud) 显然,静态类上不能有实例成员,因为该类永远不会被实例化.为什么我们需要将成员声明为静态?