1

Here is some code explanation:

abstract class parent
{
    protected void longFunction()
    {
        //........
    }

    protected void longLongLongFunction()
    {
        //........
    }

    protected abstract void changeMe();
}

class child : parent
{
    protected override void changeMe()
    {
        longFunction();
    }
}

In my particular case it is more comfortable for me to declare all possible methods inside the abstract parent class, and then the children pick-up/call only the methods that they need. So if a child calls only one method of 5 declared in the parent class, will the produced object contain the code of the 4 never called functions? If I declare the methods externally, I would have to pass down to them lot of parameters and it is much briefly to have the methods that way because they can directly use the "protected" fields of the parent class. So in the code above, will longLongLongFunction be included at compilation time in the created instance of child class, making that way the final program unnecessarily large?

I think this question is not duplicate of this question because some children will call the methods that are unused by others. So at some moment those methods will be called, and needed, but not by all children. Let's say only 5 of 10 children will call the second method. Will all 10 instances of the children include that method just because it is declared in the parent?

Community
  • 1
  • 1
  • 1
    Hope that all editions of Visual studio were having compiling facilities, it gives the answer for `will it be compilled?` – sujith karivelil Dec 28 '16 at 02:20
  • 1
    tell me how should I say it then to edit it! I know it is interpreted, but I hope ppl would understand me. People who want to understand me, would understand me. –  Dec 28 '16 at 02:26
  • @NIki Your question is fine, I don't quite understand the point unlucky is getting at - it's not uncommon for compilers to not compile statically verifiable dead code. Neither msbuild nor mono eliminate dead code, however - as per the duplicate target – Rob Dec 28 '16 at 02:27
  • Maybe VS will create only a pointer to the methods each time they are called and preserve only one explicit copy of the code itself in the final executable/interpretable, in such case my question is pointless, because there would be not a significant waste of memory. –  Dec 28 '16 at 02:35
  • 2
    @NIki Indeed, it leverages a [vtable lookup](https://en.wikipedia.org/wiki/Virtual_method_table) – Rob Dec 28 '16 at 02:43
  • One way or the other you will have to compile them. If you make your `Parent` class as a DLL and add it as reference in your `Child` class, the `Child` class won't compile `Parent` unused methods as part of the `Child` program. But you have to have the `Parent` compiled as DLL already and use it as Reference. You can make them two different projects and make the `Child` project refer to the parent project. That way the `Child` project won't compile `Parent' unused methods. – Everyone Dec 28 '16 at 03:13
  • Not sure if the compiler could be sure that a function is not called via reflection. – TaW Dec 28 '16 at 08:18
  • You are mixing two questions: _Is the same code for multiple object code repeated?_ and _Is uncalled code omitted?_ For the former [see here](http://stackoverflow.com/questions/25592562/is-lambda-code-repeated-with-dynamically-created-controls). Your title should clearly state what the real question is.. – TaW Dec 28 '16 at 10:29

0 Answers0