我正在尝试使用vector<int> myVector2,但是,在static function (foo). 我使用 Qt,下面是默认代码:
Mainwindow.h
---------------------------------------------------
#include <QMainWindow>
#include <vector>
#include <iostream>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
static std::vector<int> myVector2;
static void foo();
private:
Ui::MainWindow *ui;
};
Run Code Online (Sandbox Code Playgroud)
……
mainwindow.cpp
------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
foo;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::foo(){
MainWindow::myVector2.push_back(3);
}
Run Code Online (Sandbox Code Playgroud)
我刚刚添加static std::vector<int> myVector2; …
为什么当数据成员在类内初始化且没有类外定义时,C++ 不允许获取静态数据成员的地址?在这种情况下,静态成员的存储空间是如何分配的?
下面的最小程序演示了这个问题。
#include <iostream>
class Test {
public:
static const int a = 99; // how is the storage allocated for Test::a??
};
// const int Test::a;
int main() {
std::cout << Test::a << '\n'; // OK, print 99
const int* ptr = &Test::a; // Linker error, undefined reference to Test::a
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释该行const int Test::a,那么程序就可以正常工作。
为什么不呢
public static $CURRENT_TIME = time() + 7200;
Run Code Online (Sandbox Code Playgroud)
工作(错误):
解析错误:语法错误,意外'('
但
class Database {
public static $database_connection;
private static $host = "xxx";
private static $user = "xxx";
private static $pass = "xxx";
private static $db = "xxx";
public static function DatabaseConnect(){
self::$database_connection = new mysqli(self::$host,self::$user,self::$pass,self::$db);
self::$database_connection->query("SET NAMES 'utf8'");
return self::$database_connection;
}
}
Run Code Online (Sandbox Code Playgroud)
确实有效.
我是OOP的新手,我很困惑.
我有一个我想在2级中使用的变量.我必须将其声明为静态变量吗?它可以是实例变量吗?
public class Text extends JFrame implements ActionListener{
JTextArea t;
String s;
}
Run Code Online (Sandbox Code Playgroud)
我想在另一堂课中使用s.我必须将其声明为静态变量吗?是否可以将其声明为实例变量?
在我的程序中,当用户输入一个号码时,程序通过套接字将该号码发送到服务器,服务器发回与该号码匹配的数据.该数字代表服务级别.具有IncomingReader()实例作为其runnable的线程然后读取从服务器发送的内容,并将其存储为arraylist(详细信息).然后,我使用详细信息arraylist中的数据创建类MyClients的对象.我的问题是创建对象的循环在从服务器读取数据的线程运行之前运行.如何在创建对象的循环之前使从服务器读取的线程运行?代码如下:(我为了简洁而删除了GUI的代码)
public class SearchClients {
JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
BufferedReader reader;
PrintWriter writer;
Socket sock;
static ArrayList<String> details = new ArrayList<String>();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SearchClients window = new SearchClients();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SearchClients() {
initialize();
}
private void initialize() {
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
JButton …Run Code Online (Sandbox Code Playgroud) 这是一个相关的问题:如何检查变量是否存在?
但是,它对静态变量不起作用.
我想要做的是以下,
class A:
def __init__(self):
if A.var is null: # this does not work, okay
A.var = 'foo'
print 'assigned'
Run Code Online (Sandbox Code Playgroud)
好的,因为A.var甚至没有分配.它引起了错误.所以,我试过这个:
class A:
def __init__(self):
if 'A.var' not in globals(): # this seems to okay, but ..
A.var = 'foo'
print 'assigned'
a = A()
b = A()
Run Code Online (Sandbox Code Playgroud)
结果如下:
assigned
assigned
Run Code Online (Sandbox Code Playgroud)
这表明该if 'A.var' not in globals():行无法正常工作.
那么,我如何检查Python中是否存在静态变量?
当我将static const char[]我的类作为公共字段添加到我的类时,编译器会给我错误,但是static const int它很好.为什么,我该如何解决?
class example
{
public:
static const int num2 = 5;// fine
static const char num[] = "test";// problem
};
Run Code Online (Sandbox Code Playgroud) 数据成员的初始值设定项是否const static被视为默认成员初始值设定项?
相关的写法是[class.mem.general]/10:
大括号或等于初始化器只能出现在数据成员的声明中。(对于静态数据成员,请参阅 [class.static.data];对于非静态数据成员,请参阅 [class.base.init] 和 [dcl.init.aggr])。非静态数据成员的大括号或等于初始值设定项指定成员的默认成员初始值设定项[..]
例如:
constexpr int f() { return 0; }
struct A {
static const int I = f();
};
Run Code Online (Sandbox Code Playgroud)
大括号或等于初始化器 是否f()被视为默认成员初始化器?
当我static在班上有一个字段时:
public static int Counter = 0;
Run Code Online (Sandbox Code Playgroud)
使用static构造函数:
static Class() {
Counter++;
}
Run Code Online (Sandbox Code Playgroud)
当我创建这个类的对象并检查Class.Counter它显示我1是正确的.
但是当我创建同一个类的另一个对象时,Class.Counter仍然存在1.
这是为什么?
我刚读过(第k次)
这是关于模拟虚拟静态成员的问题.我的问题是 - 是什么让C++标准委托(或之前的Bjarne Stroustrup)没有将此功能添加到C?他们知道会破坏什么吗?或妨碍任何事情的表现(即使不使用)?
为了更好地说明我对功能定义本身的看法,这里有一些代码:
// This does not compile!
class Base {
// A pure virtual member - perhaps need to indicate that somehow
virtual static const string ToWhom;
void sayHello() {
cout << "Hello, " << ToWhom << "!" << endl;
}
};
class World : public Base {
static virtual const string ToWhom = "the entire world"s; // C++14 literal
};
class Everybody : public Base {
static virtual const string ToWhom = "everybody around"s; …Run Code Online (Sandbox Code Playgroud) 我想知道在静态类中,所有方法和数据成员应该是静态的还是可以找到非静态成员?
static-members ×11
c++ ×5
static ×3
c# ×2
java ×2
php ×1
python ×1
qt ×1
virtual ×1
visual-c++ ×1