Are class methods in C++ instantiated? - methods

I wanted to make some methods static because I didn't want thousands of copies of them, but when I did the sizeof() on my instantiated objects the extra methods in the class didn't seem to make a difference in size, only the variables did. So are the non-static methods instantiated each time? If not, why would you use static? Because I thought that static things don't get instantiated but non-static do.

Related

Dart: Mixins vs Static Methods

Is using mixins a better practice than using static methods?
For example:
we can create a Utils class, put static methods in it and then use them like Utils.print().
or we can create a UtilsMixin class, access it using "with" keyword and just call print().
How do these two methods compare to each other? Which one is the way to go?
Mixins vs Static member is like Black vs White. They do the opposite.
Members of a mixin are linked to one specific instance of an object. But static members are common to all objects
If it made sense to implement something like a static function, then it likely means that a mixin is not what you want. It'll just make the object bloated and slower to instantiate.

Difference between Free and FreeImage for TRasterImage variables?

In my Lazarus project, I use variables of
TPortableNetworkGraphic type, which is inherited from TRasterImage. Until now, I have released them with the Free method, but I have noticed that for these types also a FreeImage method exists.
What is the difference between these two? Which one should I use, or should they be called both?
If we investigate the methods we find the following:
Method Free originates from TObject. Calling it destroys your class instance.
Method FreeImage comes from TRasterImage and is equal to SetHandle(0) method from the same class. The latter technically deals mainly with FSharedImagevariable (TSharedRasterImage class instance) and its Handle property. In other words after calling FreeImage the instance of TPortableNetworkGraphic class instance will be still "alive".

Equivalence of static methods in Go

Let's say in Java, I have class CryptoFormat, which has a static method named getLegacyFormat(). When I want to use the method, I just need to call CryptoFormat.getLegacyFormat(). This is clear because I know where the method comes from.
In Go, there is no static method. I don't really want to just make a file called crypto_format.go and define the method there. The reason is that whenever I need the method, I just call GetLegacyFormat(), which doesn't contain the context where the method comes from.
I could think of two ways to solve the problem:
Make a separate package named cryptoformat, and define the method as a global function in the package. This way, I need to make a new package for just few methods. Also, whenever I need static methods like this, I have to define new packages.
Define a struct named cryptoFormat containing method GetLegacyFormat(). Also, define a global public variable named CryptoFormat, which points to an instance of struct cryptoFormat. This way, I can call CryptoFormat.GetLegacyFormat() when I need the method.
I am not sure which one is better, or whether there is better way.
I would say option 1 you mention is the more idiomatic way to define such functions, if they don't need any state that would warrant to tie them to an underlying struct.
If there is some state you'd like to have as context for the function, then option 2 would be the way to go.
Note that in Go, functions are "first class citizens", so you don't have Java's constraints of needing to define a class for static methods.
And yes, if you want a separate namespace you'd need to define separate packages (just as in Java you'd need to define separate classes and/or packages).
If you want your implementation to be idiomatic, I'd suggest you take a look at Go's standard libraries (pick a few packages and explore how they implement their functions) to get a better feeling of the usual ways to structure this.
whenever I need the method, I just call GetLegacyFormat(), which doesn't contain the context where the method comes from.
So add context to the function name.
GetLegacyCryptoFormat()

Performance gain of marking class methods as static

I am using FxCop to look for improvements on our application. One of the rules we are often breaking is "Mark members as static" which is marked as a performance rule.
Certainly we have a lot of class methods that do not action on any of the class members, which could be marked as static, but is there really a performance gain to this?
My understanding is that static will be intantiated once at execution time. If the method is never invoked that it would have been a waste. If the method is invoked multiple times than there might be a small benefit.
With variables there are obvious implications as to whether or not they are marked static, and it is critical to the operation of your application how they are defined. For methods though I don't believe there is any functional affect on whether or not they are marked static if they do not reference any instance variables or methods.
Am I missing the point here? Is the standard to mark all of these methods as static?
Performance becomes better because static method doesn't have hidden "this" pointer.
Every instance (non-static) method has hidden "this" pointer, which is passed to the method to access instance members. If no non-static members are used, "this" pointer remains unused. Passing additional parameter on the stack or in CPU register takes a time which can be saved by declaring such method as static.
"My understanding is that static will be instantiated once at execution time."
Both static and non-static methods exist only once in the program code. Only non-staic data members are duplicated, when there are different class instances. Non-static class method works with specific instance using class reference (hidden parameter). Code itself is never duplicated.
As you said, when a method is marked as static, it is instantiated once, the first time it is used. Whenever subsequent calls are made, the runtime environment doesn't need to validate it, because it is guaranteed to exist. Here is the Microsoft doc for it: http://msdn.microsoft.com/en-us/library/ms245046%28v=vs.80%29.aspx
Here's some more guidance: Method can be made static, but should it?

Visual Studio 2008 - Is there a way to set a breakpoint for when a class is accessed?

I don't really care which line in a class was hit. I just want to know when the class is accessed.
If it's an instantiable class, put a break point in the constructor. If it has static methods or properties, you would have to put a breakpoint in the first line of each method/property. As far as I know, that's the only way to break when a class is accessed.
Thats not realy how it works.You dont access file(unless your app is reading from it X-P).
You access a class and its methods/properties/contructors. These can be in different files all together (using the partial keyword), so that will make things difficult already.
What exactly are you trying to achieve? If you explain a little more, maybe then we can give a better answer.
In addition to just putting a breakpoint in all instance constructors you could also create a static constructor and put a breakpoint in it if the class is static or if it has static methods. A static constructor is guaranteed to be executed before any static method in a class.

Resources