Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the different ways of applying C++ static?

Shulou Source: shulou.com Published: 2022-06-02 22:37:58 10月05日 Update

C++ static different applications are how, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

There are many differences between C++ programming language and other languages, especially C language. Well, today, we can analyze the differences between C++ and static from the application method. At the same time, it allows you to further master the programming methods of C++ language.

C++ static specific application code example:

Public class C {public static void M () {Console.WriteLine ("call in class C");} public class D: C {public new static void M () {Console.WriteLine ("call in class D");}} public class E where T: C {public static void N () {T.M ();}}

The code is wrong and one instance is not allowed to call a static method. If you compile, it will prompt:

Error 2'T'is a 'type parameter'

Which is not valid in the given context

Why? From a language design point of view, only one of the following three cases is true for the above code.

1. It's the wrong way to write it.

2. E.N () calls C.M () no matter what T is.

3. E.N () calls C.M () but E.N () calls D.M ().

If you follow the 2 design, there will be users who expect to execute class D's method M instead of C when T is class D. Static is a static because it can be exactly determined at compile time, or this method can be determined during the static code analysis phase. Therefore, if we design according to the way of 3, we will violate this principle. In this way, there is only one left.

Another explanation:

1. Virtual static, why is there no such thing?

two。 There is no this pointer (the above is transferred from a colleague's blog with a simple modification)

However, it is not clear why C++ is allowed to do so.

Public class Test {public static void Say () {}} Test t; Test* T2 = new Test (); t.Say (); T2-> Say (); after reading the above, have you mastered how C++ static is applied in different ways? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Ways applications methods languages differences code content E.N design more errors problems faceted C.M analysis programming compilation clarity helplessness Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information OPPO Reno Linux Apple