static-methods

Static variables in static method in base class and inheritance

故事扮演 提交于 2019-11-30 15:21:17
问题 I have these C++ classes: class Base { protected: static int method() { static int x = 0; return x++; } }; class A : public Base { }; class B : public Base { }; Will the x static variable be shared among A and B , or will each one of them have it's own independent x variable (which is what I want)? 回答1: There will only be one instance of x in the entire program. A nice work-around is to use the CRTP: template <class Derived> class Base { protected: static int method() { static int x = 0;

Why would I use static methods for database access

醉酒当歌 提交于 2019-11-30 15:18:08
问题 So I came across this issues today and I couldn't find some meaningful explanation is there some non-subjective reason to use static methods when it comes to database interactions. Right now I'm working on a project where everything is made through stored procedures and for example I have some regular methods like : public void Delete(int clientID) { //calling store procedure } or public void Save(int clientID) { //calling store procedure } but I also have : public static Client GetByID(int

Why would I use static methods for database access

房东的猫 提交于 2019-11-30 13:51:40
So I came across this issues today and I couldn't find some meaningful explanation is there some non-subjective reason to use static methods when it comes to database interactions. Right now I'm working on a project where everything is made through stored procedures and for example I have some regular methods like : public void Delete(int clientID) { //calling store procedure } or public void Save(int clientID) { //calling store procedure } but I also have : public static Client GetByID(int id) { //calling stored procedure } or public static int AddNew(string firstName, string lastName...) { /

Reflection on a static overloaded method using an out parameter

戏子无情 提交于 2019-11-30 13:13:07
问题 I'm having some issues with invoking an overloaded static method with an out parameter via reflection and would appreciate some pointers. I'm looking to dynamically create a type like System.Int32 or System.Decimal , and then invoke the static TryParse(string, out x) method on it. The below code has two issues: t.GetMethod("TryParse", new Type[] { typeof(string), t } ) fails to return the MethodInfo I expect mi.Invoke(null, new object[] { value.ToString(), concreteInstance }) appears to

Calling a static method from a class in another namespace in PHP

点点圈 提交于 2019-11-30 12:08:15
This code bellow gives me this error : Class 'MyNamespace\Database' not found . How do I reference a class that belongs to no namespace, from inside one ? Class Database { public function request() { } } namespace MyNamespace { class MyClass { public function myFuction() { Database::request(); } } } Try with \Database::request(); Also see Namespace Basics Example 1 in the PHP Manual 来源: https://stackoverflow.com/questions/6579040/calling-a-static-method-from-a-class-in-another-namespace-in-php

Why PHP uses static methods in object context?

≯℡__Kan透↙ 提交于 2019-11-30 11:35:24
I have the following code (like, for real, this is my real code) : <?php class Foobar { public static function foo() { exit('foo'); } } When I run $foobar = new FooBar; $foobar->foo() it displays foo . Why would PHP try to use a static method in an object context ? Is there a way to avoid this ? Ok you guys didn't get my problem : I know the differences between static and non static methods and how to call them. That's my whole point, if I call $foobar->foo() , why does PHP tries to run a static method ? Note : I run PHP 5.4.4, error reporting to E_ALL . Fenton To call a static method, you don

Why can I only access static members from a static function?

元气小坏坏 提交于 2019-11-30 11:32:06
I have a static function in a class. whenever I try to use non static data member, I get following compile error. An object reference is required for the nonstatic field, method, or property member Why is it behaving like that? A non-static member belongs to an instance. It's meaningless without somehow resolving which instance of a class you are talking about. In a static context, you don't have an instance, that's why you can't access a non-static member without explicitly mentioning an object reference. In fact, you can access a non-static member in a static context by specifying the object

Static Vs Instance Method Performance C#

给你一囗甜甜゛ 提交于 2019-11-30 11:28:00
问题 I have few global methods declared in public class in my ASP.NET web application. I have habit of declaring all global methods in public class in following format public static string MethodName(parameters) { } I want to know how it would impact on performance point of view? Which one is better? Static Method or Non-Static Method? Reason why it is better? http://bytes.com/topic/c-sharp/answers/231701-static-vs-non-static-function-performance#post947244 states: because, static methods are

C# interface cannot contain operators

依然范特西╮ 提交于 2019-11-30 10:48:28
Can anyone please explain why C# interfaces are not allowed to contain operators? Thanks. dkackman C# operators have to be static . Interfaces, by definition, apply to instances. There is no mechanism to require a type to implement static methods. 来源: https://stackoverflow.com/questions/6603940/c-sharp-interface-cannot-contain-operators

How do I call a static method of another class

烈酒焚心 提交于 2019-11-30 10:44:21
I have a class, lets say CAppPath which has a static method: public: static CString GetAppPath(); and in CAppPath.cpp it's defined as: CString CAppPath::GetAppPath() { return "C:\..\MypAth"; } Now I have another class CXMLHandler , and I have included CAppPath.h in it. But how do I call the GetAppPath() method? I've tried: #include "CAppPath.h" void CXMLHandler::MyMethod { CNDSClientDlg->GetAppPath(); } but it doesn't work. How should I access this method? Since it is a static method, do I need to create a object of the class or should I make the class itself static? You only need to use the