NeuralJack
Centurion
- Joined
- Jul 28, 2005
- Messages
- 138
Let's say you have a base class that 5 other classes inherit
cls_Bob_Base
cls_Bob_1 : Inherits cls_Bob_Base
cls_Bob_2 : Inherits cls_Bob_Base
cls_Bob_3 : Inherits cls_Bob_Base
cls_Bob_4 : Inherits cls_Bob_Base
cls_Bob_5 : Inherits cls_Bob_Base
Now in the cls_Bob_Base you have a public non-overrided function called DoThis(). Performing DoThis() is done the same in every Bob class. My question is this:
When you instantiate 10 Bob classes it's probably going to create 10 DoThis() functions in your system memory isnt it?
Really, it only needs one memory instance of DoThis(), so it seems wasteful to even make non-overrided base functions. But, I'm pretty sure that cls_Bob_Base is never actually instantiated, anywhere, so that means DoThis() will have to be instantiated on each instance of the other bob classes.
I suppose you have to make a judgement call on whether to just declare DoThis() somewhere completely different than the base class based on readability vs how much memory you want to waste needlessly. I guess it may not matter too much because my classes probably dont take up much memory at all.
I wonder about this often even during regular non-inherited class construction too. There are many functions/subs in the class that could be done outside the class, in fact *most could* be done outside the class, and then there'd only be one copy of the function/Sub in memory.
If you have any general guidelines -or tutorials- on when and when-not to make a function/sub inside a class, let me know. Right now, my only guildlines are to do it to avoid casting outside the class. Readability might be another good reason.
Here is another example of what i'm talking about:
If you have 200 instances of each Cls_1, Cls_2, Cls_3 Then you are
comparing 600 memory copies of DescribeMe() in This:
Verses 1 memory copy of this function on your main form or something:
cls_Bob_Base
cls_Bob_1 : Inherits cls_Bob_Base
cls_Bob_2 : Inherits cls_Bob_Base
cls_Bob_3 : Inherits cls_Bob_Base
cls_Bob_4 : Inherits cls_Bob_Base
cls_Bob_5 : Inherits cls_Bob_Base
Now in the cls_Bob_Base you have a public non-overrided function called DoThis(). Performing DoThis() is done the same in every Bob class. My question is this:
When you instantiate 10 Bob classes it's probably going to create 10 DoThis() functions in your system memory isnt it?
Really, it only needs one memory instance of DoThis(), so it seems wasteful to even make non-overrided base functions. But, I'm pretty sure that cls_Bob_Base is never actually instantiated, anywhere, so that means DoThis() will have to be instantiated on each instance of the other bob classes.
I suppose you have to make a judgement call on whether to just declare DoThis() somewhere completely different than the base class based on readability vs how much memory you want to waste needlessly. I guess it may not matter too much because my classes probably dont take up much memory at all.
I wonder about this often even during regular non-inherited class construction too. There are many functions/subs in the class that could be done outside the class, in fact *most could* be done outside the class, and then there'd only be one copy of the function/Sub in memory.
If you have any general guidelines -or tutorials- on when and when-not to make a function/sub inside a class, let me know. Right now, my only guildlines are to do it to avoid casting outside the class. Readability might be another good reason.
Here is another example of what i'm talking about:
If you have 200 instances of each Cls_1, Cls_2, Cls_3 Then you are
comparing 600 memory copies of DescribeMe() in This:
Code:
Public Class cls_Base
Public Mustoverride Function DescribeMe() as String
End Class
Public Class cls_1 : Inherits cls_Base
Public Function DescribeMe() as String
Return "I am class 1"
End Function
End Class
Public Class cls_2 : Inherits cls_Base
Public Function DescribeMe() as String
Return "I am class 2"
End Function
End Class
Public Class cls_3 : Inherits cls_Base
Public Function DescribeMe() as String
Return "I am class 3"
End Function
End Class
Verses 1 memory copy of this function on your main form or something:
Code:
Public Function Describe_cls(in_cls as cls_Base) as string
If typeof in_cls is Class cls_1
Return "I am class 1"
ElseIf typeof in_cls is Class cls_2
Return "I am class 2"
ElseIf typeof in_cls is Class cls_3
Return "I am class 3"
End If
End Function