我一直在阅读一些有关静态函数和静态成员函数的内容。根据我的理解,如果一个函数被声明为静态,那么该函数仅对其翻译单元可见,而对其他地方不可见。相反,静态成员函数是无需实例化其类的任何对象即可调用的函数(因此您可以像在名称空间中一样使用它)。
为了用静态函数来澄清,我的意思是
static int foo(int a, int b)
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
对于静态成员函数,我的意思是
struct MyClass
{
static int foo(int a, int b)
{
return a + b;
}
}
Run Code Online (Sandbox Code Playgroud)
这是唯一的区别吗?或者同一翻译单元内的可见性仍然是他们两个的共同特征?
所以我最近发现了一些使用特定技术的源代码(成语?)我以前没见过; 简而言之; 它不是使用相关类的静态变量,而是在类源文件中使用局部变量.
myclass.h
class myclass {
//static int myint;
public:
myclass();
~myclass();
int count();
};
Run Code Online (Sandbox Code Playgroud)
myclass.cpp
#include "myclass.h"
int myint = 0;
myclass::myclass() {
myint++;
}
myclass::~myclass() {
myint--;
}
int myclass::count() {
return myint;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "myclass.h"
#include <iostream>
int main() {
myclass aclass;
myclass theclass;
std::cout << theclass.count(); //outputs 2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么有人会采用这种方法而不是使用静态变量?
我对它的看法是,理想情况下,变量只能为myclass类(私有静态)所知,并且继承根本不重要(在这种情况下),这可能会阻止其他人知道这个变量.但这是我能看到的唯一优势; 不确定这是否值得保证.
同样的问题适用于私有的(静态/非静态)成员函数; 当继承不重要时.
编辑:读完之后,我要做的是因为有些人仍然使用C编程风格...
我知道C++和Java,我不熟悉Pythonic编程.所以也许这是我想要做的坏风格.
考虑下面的例子:
class foo:
def a():
__class__.b() # gives: this is foo
bar.b() # gives: this is bar
foo.b() # gives: this is foo
# b() I'd like to get "this is bar" automatically
def b():
print("this is foo")
class bar( foo ):
def b( ):
print("this is bar")
bar.a()
Run Code Online (Sandbox Code Playgroud)
请注意,我没有使用self参数,因为我没有尝试创建类的实例,因为不需要我的任务.我只是试图以一种可以覆盖函数的方式引用函数.
这是一个很好的例子:我正在尝试重载OpenGL,glutMouseFunc因此它可以接受我选择的命名空间和类功能.特别是Init::DisplayInit::mouse,这是静态的.问题是,这可能吗?如果是这样,这是如何实现的?
我的实施
void glutMouseFunc(void (Init::DisplayInit::*mouse)(int, int, int, int)) {
(*mouse);
}
Run Code Online (Sandbox Code Playgroud)
实施中的错误
..\OpenGL_03\/displayinit.h:27: error: variable or field 'glutMouseFunc' declared void
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: void value not ignored as it ought to be
Run Code Online (Sandbox Code Playgroud)
注意,我把函数的声明放在同一个文件的头文件中.我还确保函数的声明和定义都位于名称空间声明之外(它包含了大部分文件,每个文件).如图所示,第一个错误之一将函数读作变量或字段(???).
我有一个包含一些静态函数的 C 文件,如何使用 google test 来测试那些静态函数?
头文件:
test.h
int accessData();
Run Code Online (Sandbox Code Playgroud)
源文件:
test.c
static int value;
static int getData()
{
return value;
}
int accessData()
{
if(value != 0)
{
return getData();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
静态函数被全局函数调用,但是如何使用谷歌测试来测试那些静态函数?
我正在使用 laravel 5。在模型中,我有一个静态函数,我在控制器中调用它。它工作正常,但我希望这个函数与另一个非静态函数进行相同的更改,当我在静态函数内调用它时,它会产生错误。
Non-static method App\Models\Course::_check_existing_course() should not be called statically
Run Code Online (Sandbox Code Playgroud)
这是我的模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model {
public $course_list;
protected $primaryKey = "id";
public function questions(){
return $this->belongsToMany('App\Models\Question','course_questions')->where("status",1)->orderBy("id","DESC");
}
public static function courses_list(){
self::_check_existing_course();
}
private function _check_existing_course(){
if(empty($this->course_list)){
$this->course_list = self::where("status",1)->orderBy("course")->get();
}
return $this->course_list;
}
}
Run Code Online (Sandbox Code Playgroud) 我想在kotlin中声明一个扩展函数,但是在Java类库上,当您companion在扩展函数中解析时,我知道在Kotlin中这样做。喜欢:
class Food {
companion object {
fun foo() = Unit
}
}
fun Food.Companion.clear(){/*Clear all of objects*/}
Run Code Online (Sandbox Code Playgroud)
现在,有什么方法可以static在Java类库中注入函数吗?
请帮忙,
问题:以下代码中的核心转储:
我有一个抽象类 SomeOtherClass,并从它派生了 SomeOtherClassImpl。
这是导致问题的代码:
class MyClass
{
public:
void someFunction()
{
myVector().push_back(someOtherClassDefault());
}
private:
static std::vector<SomeOtherClass const *> & myVector()
{
static std::vector<SomeOtherClass const *> theVector;
return theVector;
}
static SomeOtherClass const * someOtherClassDefault()
{
static SomeOtherClassImpl theDefault;
return &theDefault;
}
};
Run Code Online (Sandbox Code Playgroud)
我在其他翻译单元中有一些 MyClass 类型的静态变量。
这个问题很奇怪,因为程序退出时会发生分段错误。当然 theDefault 可以在 theVector 之前被释放,但是有什么区别呢?当 main 已经完成时,两者都被解除分配。
您的帮助将不胜感激。
我有以下类定义和main().有人可以指出我为什么会收到错误吗?
#include <iostream>
#include <list>
using namespace std;
class test
{
protected:
static list<int> a;
public:
test()
{
a.push_back(150);
}
static void send(int c)
{
if (c==1)
cout<<a.front()<<endl;
}
};
int main()
{
test c;
test::send(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误如下:
/tmp/ccre4um4.o: In function `test::test()':
test_static.cpp:(.text._ZN4testC1Ev[test::test()]+0x1b): undefined reference to `test::a'
/tmp/ccre4um4.o: In function `test::send(int)':
test_static.cpp:(.text._ZN4test4sendEi[test::send(int)]+0x12): undefined reference to `test::a'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
即使我使用c.send(1)而不是test :: send(1),错误也是一样的.在此先感谢您的帮助.
希望这不是与 C 中“静态”函数的含义相关的许多其他问题的重复。
我们支持一些包含以下内容的传统原生 C 代码......(不要问我为什么要重新 VOID/void)
#define VOID void
#define LOCAL static
...
VOID LOCAL vLoMyMethod();
Run Code Online (Sandbox Code Playgroud)
据推测,预处理器将后者转换为
void static vLoMyMethod();
Run Code Online (Sandbox Code Playgroud)
编译器(Visual Studio 2015,大概是相对普通的标志/设置,警告级别 W3)对此似乎没问题,尽管我的理解是“静态”应该出现在返回类型说明符之前,即
static void vLoMyMethod();
Run Code Online (Sandbox Code Playgroud)
这些在语法上是否相同并且都正确?如果不是,为什么编译器会接受前者可能不正确的语法?
编辑 1
到目前为止,感谢您的回答。有趣的是,我不确定它们是否 100% 等效并且在所有情况下都可以接受,ala:
char * static vLoMyMethod1(); // compiler complains about expecting 'type' (intellisense wants an identifier)
static char * vLoMyMethod2(); // compiler is fine
Run Code Online (Sandbox Code Playgroud) 为什么this在静态成员函数中不允许未评估的上下文?
struct A
{
void f() {}
static void callback(void * self) // passed to C function
{
static_cast< decltype(this) >(self)->f();
}
};
Run Code Online (Sandbox Code Playgroud)
此代码给出错误:
错误:'this'不适用于静态成员函数
Run Code Online (Sandbox Code Playgroud)static_cast< decltype(this) >(self)->f(); ^~~~
decltype(this)为了简洁需要(有时它会短得多VeryVeryLongClassName *),另一个优点是意图更清晰.
标准说什么this在静态成员函数中使用未评估的上下文?
我正在阅读有关静态函数的内容,据说如果该函数是静态的,那么您只能在同一个文件中使用它。测试后,我意识到这不是真的,因为如果您包含带有静态函数的文件,您仍然可以在另一个文件中使用该函数。然后我读到一个说明,你实际上只能在同一个翻译单元中使用静态函数。好的,这是有道理的,因为它意味着 .cpp + 包含,但即使该函数不是静态的,除非您包含该文件,否则您仍然无法使用它。那么怎么可能首先从另一个翻译单元访问一个函数而不包含任何东西,静态函数有什么意义呢?
主程序
#include "Staticf.h"
void main()
{
visible();
}
Run Code Online (Sandbox Code Playgroud)
静态文件
#pragma once
#include <iostream>
using namespace std;
static void visible()
{
cout << "Static function is visible\n";
}
Run Code Online (Sandbox Code Playgroud)
这编译没问题。如果我将该函数设为非静态并删除 #include "Staticf.h" 我将无法在 Main 中使用它。那么,如果您也不能访问非静态函数,为什么还需要静态函数呢?
static-functions ×12
c++ ×7
c ×2
android ×1
c++14 ×1
c++17 ×1
class-method ×1
glut ×1
googletest ×1
inheritance ×1
java ×1
kotlin ×1
laravel-5 ×1
oop ×1
php ×1
python ×1