I have a doubt about which is better from performance point of view.
I have a variable of type aViewController and it contains an object of bViewController.
so every time i need to call a method on bViewController I call it like (bViewController *)aViewController.MethodName
I am thinking if storing bViewController in variable of its own type and using that in place typecasting it will be any better.
what is the overhead associated with typecasting?
You don’t appear to be following standard naming conventions, hence the confusion apparent in the comments. This is my guess at what you doing:
You say you have:
Nothing wrong with that – the variable name starts with a lowercase letter. You then say you do:
Here
bViewControlleris presumably a class and so by convention should start with an uppercase letter. You also don’t say how this class is defined but presumably it is a subclass (correcting the case):Now after the assignment above you wish to call a method defined in
BViewControllerand so write:((BViewController *)aViewController).propertyDefinedInBViewController
First calling a method is not (usually) done with the dot syntax, that is for properties. Second you need the extra parentheses otherwise you are casting the result of the property call and not
aViewController.Does that fit what you’re doing?
If so, to your question – what is the cost of the typecast?
A typecast from one pointer type to another costs nothing, while a typecast between value types (integers, real numbers, etc.) may involve a run time conversion and carry a cost. Your case is the former so the cost is zero at runtime. However if you’re making a lot of references I would recommend you use an intermediate variable of the correct type and cast just once, this tends to improve readability (development time cost). Also you should not cast to a subclass unless you’ve done a check you actually have an instance of the class, e.g.:
or you code guarantees you have an instance of the subclass and that is obvious or commented.
HTH and I’ve not completely guessed wrong what you’re doing!