标签: static-members

静态方法调用

每个静态调用都会启动一个新线程吗?

对于Eg:

class A
{
    public static void displayName()
    {
        Console.WriteLine("myName");
    }

    public static void displayAge()
    {
        Console.WriteLine("myAge");
    }
}


class B
{
    public void Foo()
    {
        A.displayName();
        A.displayAge();           
    }
} 
Run Code Online (Sandbox Code Playgroud)

上述调用是否会相互独立?如果是,那么它是否类似于线程?

c# static-members

1
推荐指数
1
解决办法
258
查看次数

C++ - 为什么这个成员需要声明为static?

这让我疯了.我得到一个"没有匹配函数调用'WordCloud :: WordCloud()'"错误.似乎正在调用默认构造函数,但由于我没有定义一个,因此抛出了错误.

构造函数是:

WordCloud( map<string, int> *source ); 
Run Code Online (Sandbox Code Playgroud)

在主cpp文件中,错误发生在指示的行上

class FontTestingApp : public AppBasic 
{                                      // <-- error was appearing on this line
public:
  void setup();
  void mouseDown( MouseEvent event ); 
  void update();
  void draw();

  map<string, int> wordList;
  WordCloud wc;        // comment out this line and it compiles
};
Run Code Online (Sandbox Code Playgroud)

所以,我猜测在实例化FontTestingApp类时实例化了一个WordCloud对象.

如果我注释掉这条线

WordCloud wc;
Run Code Online (Sandbox Code Playgroud)

然后它编译.

为了解决它,我终于将线路改为

static WordCloud wc;
Run Code Online (Sandbox Code Playgroud)

那编译.

我真的不知道为什么,而且这还不够好:)如果有人能解释这里发生了什么,我会非常感激.我顺便使用libCinder(libcinder.org)

c++ static-members

1
推荐指数
1
解决办法
309
查看次数

返回实例变量V/s返回直接对象

我正在研究在2009年建立的网站的源代码,它是一个自定义框架.

有什么不同?

<?php
class DbAccess {

    private static $instance;

    /**
     * Returns the instance of the DB Class
     */

    public static function getInstance()
    {
        self::$instance = new DbAccess();
        return self::$instance;
    }
}
Run Code Online (Sandbox Code Playgroud)

V /秒

<?php
class DbAccess {


    /**
     * Returns the instance of the DB Class
     */

    public static function getInstance()
    {
        return new DbAccess();
    }

}
Run Code Online (Sandbox Code Playgroud)

我已经开发了几个定制的框架和一组具有不同模式的库,但有时,我看到返回实例的方法是通过self::$instance,有时,它直接返回通过new

哪个是好习惯?考虑即将推出的PHP版本.

php static-members

1
推荐指数
1
解决办法
129
查看次数

struct static member意思/定义

struct a{static int z;}l;
(a is declared at file scope)    
Run Code Online (Sandbox Code Playgroud)

我无法使用初始化列表初始化z.静态结构成员是什么意思?

z(名称)也有外部链接和公共访问吗?

(我认为这意味着你给它的文件范围和组它下(并且经过对象的公共访问)?..为什么不能我初始化?)

另外....如果我在一个类中有一个静态结构成员?

c++ static struct static-members visual-c++

1
推荐指数
1
解决办法
2万
查看次数

理解类的地图字段上的最终静态用法

我是c ++开发人员,并试图熟悉Core Java概念.我发现这个令人困惑的是我的最终静态是在构造对象后无法改变的东西.(如果我错了,请纠正我)

我遇到了下面的实现,我发现这令人困惑,因为它似乎允许将值添加到Map中,即使它是最终静态的

public class MockUserServiceImpl implements UserService {

    private static final Map<String, User> userMap = new HashMap<String, User>();

    public static void addUserToCache(int userId, String userName) {
        if (!userMap.containsKey(userName)) {
            userMap.put(userName, new User(userId, userName));
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

有人可以试着解释一下这里的静态决赛是什么意思

java encapsulation final static-members

1
推荐指数
1
解决办法
349
查看次数

为什么模板类的静态成员不是初始化的?

template <typename T>
class A
{
    public:
    A() {p = this;}
    static A *GetP() {return p;}

    private:
    static A *p;
    static A instance;
}

template <typename T>
A<T> *A<T>::p = (A<T> *)4534;    //just test value to check whether p is initiallized or not.

template <typename T>
A<T> A<T>::instance;
Run Code Online (Sandbox Code Playgroud)

我打电话A<int>::GetP().我希望它返回实例的地址,但它返回(A<int> *)4534.另外,没有调用构造函数.

我认为这意味着p初始化很好,但实例不是.

但是,如果我特意把它像这样,

A<int> A<int>::instance
Run Code Online (Sandbox Code Playgroud)

它运作良好.为什么会出现这种现象?

c++ templates static-members

1
推荐指数
1
解决办法
99
查看次数

如何在c ++中正确清理静态类

我需要一个单例类,因为我只需要初始化一些变量.当我崩溃时,我无法清理班级时遇到问题.这是我班级的简化版本:

#include "stdafx.h"
#include <iostream>

class MyClass
{
public:
    MyClass();
    virtual ~MyClass();

    static MyClass& Instance();
    void DoSomething();
};

MyClass::MyClass()
{
    std::cout<<"MyClass constructor"<<std::endl;
    //initialise some stuff here
}

MyClass::~MyClass()
{
    std::cout<<"MyClass destructor"<<std::endl;
    //clean up some stuff here
}

MyClass& MyClass::Instance()
{
    static MyClass _instance;
    return _instance;
}

void MyClass::DoSomething()
{
    std::cout<<"Do something"<<std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    MyClass& myClass = MyClass::Instance();
    myClass.DoSomething();

    delete &myClass;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我调用delete&myClass时,析构函数会被调用,然后它会像这样爆炸:

在此输入图像描述

我已经调查过,我认为我不应该在一个尚未使用new创建的对象上调用delete.它是否正确??

答案是否只是不使用任何删除并让析构函数在超出范围时自动调用(当主要返回时)?

c++ static-members c++11

1
推荐指数
1
解决办法
195
查看次数

在c ++中创建一个静态初始化类

我希望有一个用于一次性初始化的类,如下所示:

class Initialise
{
public:
    Initialise()
    {
        m_name = "Jimmy";
    }
    ~Initialise(){}

private:
    std::string m_name;
};

class SomeClass
{
    static Initialise staticStuff; // constructor runs once, single instance
};

int main()
{
    SomeClass testl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行上面的命令时,我发现'Initialise'类的构造函数永远不会在调试器中被命中.为什么是这样?

c++ oop static-members

1
推荐指数
1
解决办法
43
查看次数

单例类成员变量的生命周期是多少?

class Member {
public:
    Member(int idx) {
        this->idx_ = idx;
    }

    int get_idx() {
        return idx_;
    }

    void set_idx(int idx) {
        this->idx_ = idx;
    }

    void foo();

private:
    int idx_;
};


class SingletonClass {
public:
    static SingletonClass& GetInstance() {
        static SingletonClass inst;
        return inst;
    }

    vector<Member*> get_members() {
        return this->members_;
    }

    Member* get_member(int idx) {
        return this->members_[idx];
    }

    void add_member(Member* mem) {
        this->members_.push_back(mem);
    }

private:
    SingletonClass() {}
    vector<Member*> members_;
};

void Member::foo() {
    SingletonClass inst = SingletonClass::GetInstance();
    cout << inst.get_members().size(); …
Run Code Online (Sandbox Code Playgroud)

c++ memory singleton static-members

1
推荐指数
1
解决办法
373
查看次数

Javascript:在递归函数中初始化一次变量(如静态变量)

我有一个递归函数,它返回树的叶节点(以嵌套对象的形式):

var retrieve = function (a)
{
    if (typeof retrieve.names == 'undefined') //would be true for the 1st pass only
        retrieve.names = []
    if (a.left != null)
        retrieve (a.left)
    if (a.right != null)
        retrieve (a.right)
    if (a.left == null && a.right == null)
        retrieve.names.push(a)
    return retrieve.names
}
Run Code Online (Sandbox Code Playgroud)

这个函数的问题是,它对单个对象(树)完全正常,但是当在参数中传递另一个对象时,它只是将叶节点附加到已经从前一个树获得的叶节点.

例如,

// gets the leaf nodes of obj1 tree
obj1_leaves = retrieve(obj1) 

// instead of only getting leaf nodes of obj2, it appends them to leaf nodes of obj1
obj2_leaves = retrieve(obj2) …
Run Code Online (Sandbox Code Playgroud)

javascript recursion static-members

1
推荐指数
2
解决办法
1281
查看次数