I am trying to create a static method that would return an instance of the class, something like:
The problem I am having is that if I have a subclass B derived from A, I would like B.getInstance() to return an instance of B, and not A. In PHP world, you could use a keyword 'self' to reference to your own type, so your getInstance() would look like:
Arrow functions do not have their own this.They are not well suited for defining object methods. Arrow functions are not hoisted. They must be defined before they are used. Using const is safer than using var, because a function expression is always constant value. You can only omit the return keyword and the curly brackets if the function is a single statement.
What's the best way to go about this?
m1tk4m1tk4
6 Answers
You can hide and so in a way override the getInstance method in the inheriting type like this:
The
new
keyword specifies that the hiding of the inherited method was intentional, otherwise you get a compiler warning.Complete example:
Dio F1,78511 gold badge1616 silver badges3535 bronze badges
Paul SasikPaul Sasik64.1k1515 gold badges122122 silver badges171171 bronze badges
You can't, basically. If a call to a static member which is only declared in a base class is actually expressed in terms of the derived class, like this:
then the compiler silently transforms that to:
The fact that the original source contained the name of a derived class is not preserved in the compiled code, so it can't be acted on at execution time.
Jon SkeetJon Skeet1114k708708 gold badges81198119 silver badges85328532 bronze badges
How about using generics to provide the type to produce:
George DuckettGeorge Duckett25k55 gold badges8383 silver badges144144 bronze badges
There's no way to do this statically -- you just have to use the actual name.
However, I'll bet PHP does this dynamically (i.e., with reflection) -- in which case you can always use
GetCurrentMethod
to get information about the method and itse enclosing type.That way you can do something like:
MehrdadMehrdad131k9494 gold badges423423 silver badges767767 bronze badges
Since others say there's no way to do this, I'll just offer up CRTP. It has its faults (Eric Lippert has a post about it here somewhere), but it will solve this problem
This does make
Base<T>
hard to use, however. It's best if Base<T>
is abstract. You can also create a side class like this that can actually be instantiated and used:It looks weird, but it works. Unfortunately,
siridesirideBase
and Derived
will not be related anymore (except by Base<T>
) so you can't cast between them, as such.136k44 gold badges2424 silver badges5050 bronze badges
You can not override static member function in derived class. If you choose static approach, you need just add the same method to B class.
Sure you can use also different design patterns, like Factory for example, but I think this is out of scope of the question.
TigranTigran55.9k66 gold badges7070 silver badges107107 bronze badges
Not the answer you're looking for? Browse other questions tagged c# or ask your own question.
I’m sure that this post will be as interesting and informative to C virgins (i.e. beginners) as it will be to those who are well versed in C. So let me start by saying that extern keyword applies to C variables (data objects) and C functions. Basically extern keyword extends the visibility of the C variables and C functions. Probably that’s is the reason why it was named as extern.
Though (almost) everyone knows the meaning of declaration and definition of a variable/function yet for the sake of completeness of this post, I would like to clarify them. Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about the declaration. Coming to the definition, when we define a variable/function, apart from the role of the declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a superset of the declaration. (or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function). So that’s all about declaration and definition.
Now coming back to our main objective: Understanding “extern” keyword in C. I’ve explained the role of declaration/definition because it’s mandatory to understand them to understand the “extern” keyword. Let us first take the easy case. Use of extern with C functions. By default, the declaration and definition of a C function have “extern” prepended with them. It means even though we don’t use extern with the declaration/definition of C functions, it is present there. For example, when we write.
There’s an extern present at the beginning which is hidden and the compiler treats it as below.
Same is the case with the definition of a C function (Definition of a C function means writing the body of the function). Therefore whenever we define a C function, an extern is present there in the beginning of the function definition. Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function can be added in several C/H files or in a single C/H file several times. But we notice the actual definition of the function only once (i.e. in one file only). And as the extern extends the visibility to the whole program, the functions can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program). So that’s all about extern with C functions.
Now let us then take the second and final case i.e. use of extern with C variables. I feel that it more interesting and informative than the previous case where extern is present by default with C functions. So let me ask the question, how would you declare a C variable without defining it? Many of you would see it trivial but it’s an important question to understand extern with C variables. The answer goes as follows.
Here, an integer type variable called var has been declared (remember no definition i.e. no memory allocation for var so far). And we can do this declaration as many times as needed. (remember that declaration can be done any number of times) So far so good. ?
Now how would you define a variable? Now I agree that it is the most trivial question in programming and the answer is as follows.
Here, an integer type variable called var has been declared as well as defined. (remember that definition is the superset of declaration). Here the memory for var is also allocated. Now here comes the surprise, when we declared/defined a C function, we saw that an extern was present by default. While defining a function, we can prepend it with extern without any issues. But it is not the case with C variables. If we put the presence of extern in a variable as default then the memory for them will not be allocated ever, they will be declared only. Therefore, we put extern explicitly for C variables when we want to declare them without defining them. Also, as the extern extends the visibility to the whole program, by using the extern keyword with a variable we can use the variables anywhere in the program provided we know the declaration of them and the variable is defined somewhere.
Now let us try to understand extern with examples.
Example 1:
Analysis: This program is compiled successfully. Here var is defined (and declared implicitly) globally.
Example 2:
Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems arise.
Example 3:
int main( void ) var = 10; } |
Analysis: This program throws an error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.
Example 4:
extern int var; { return 0; |
Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.
Example 5:
int main( void ) var = 10; } |
Analysis: Guess this program will work? Well, here comes another surprise from C standards. They say that..if a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.
So that was a preliminary look at “extern” keyword in C.
I’m sure that you want to have some take away from the reading of this post. And I would not disappoint you. ?
In short, we can say
In short, we can say
![This c# keyword This c# keyword](https://matthewproctor.com/content/json/paste-json-as-csharp-class.jpg)
1. A declaration can be done any number of times but definition only once.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible throughout the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as the definition of the variable as well.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible throughout the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as the definition of the variable as well.