4

Consider you have this 2 simple classes:

class Parent {
public:
    virtual void foo() = 0;
};

class Child : public Parent {
    std::vector<Parent*> parents;
public:
    Child() : parents() {}
    virtual void foo() {
        std::cout << "Foo" << std::endl;
    }
};

It seems correct to me! But my teacher says since I did not explicitly run the Parent's constructor in the Child's constructor, although it has no arguments, that that is a bad programming practice!

To clarify he wanted me to do this instead:

class Child : public Parent {
    std::vector<Parent*> parents;
public:
    Child() : parents(), Parent() {}
    virtual void foo() {
        std::cout << "Foo" << std::endl;
    }
};

I am failing to find any C++ specific documentation saying that what I did is considered a bad programming practice or what he wants is considered a good programming practice!

Hell! He even extended his theory to Java, that I program for more than 4 years now and never ever did this!

Can someone help me find an official document saying otherwise, or supporting my point? Or at least the experienced programmers out there that always respect the conventions, what is your opinion!?

Because for me what I did is a good programming practice, and that I shouldn't be penalized in my exam for not doing what he wanted.

  • 4
    We are not dispensers of justice. But Stack Overflow already has some good answers to the question: what-are-the-rules-for-calling-the-superclass-constructor. If that's not good enough please edit the question to make clear why it's not. – candied_orange Mar 7 '17 at 20:20
  • 3
    Not that some "convention" may be very different between teacher and professionals. – Walfrat Mar 7 '17 at 21:21
  • 3
    Short answer: do what your teacher demands for as long as necessary, but absent specific reasons to do otherwise, avoid this in real code. – Jerry Coffin Mar 7 '17 at 21:46
10

There is a semantic difference which is unique to C++. It depends on what the parent class is. If you do not call the constructor explicitly, it will be default-initialized, which may not be initialization at all for primitive types that are members of a simple type (UDTs with constructors will have their constructors called). If you explicitly call the constructor you will receive the default constructor for a UDT or value-initialization, which zeros out any primitive members.

As far as I'm aware, this distinction does not exist in other languages. Various revisions of C++ have had a few different behaviours here as well.

The typical response is to not write types that don't properly construct themselves, in which case default-initialization is perfectly adequate. However when using from unknown classes or unreliable third-party classes, it can be a best practice to defensively force value-initialization.

See https://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new for another example of where this can matter with new.

In the general case, whilst I reject your implied notion that 4 years of Java counts as some kind of useful experience, I agree that there's nothing wrong with not explicitly calling base class constructors when you are satisfied with the behaviour of the default constructor.

2

Your teacher's opinion varies from modern professional practice. Do not write unnecessary code. It just makes an expert wonder "Why is that there?" When the answer is "no good reason" the expert just thinks "WTFF?"

Many CS professors are disconnected with consensus expert practice. If you want to see how programming is really done, look at the code from important open-source projects. For C++ you could look at the source for STL or Boost.

Keep in mind that actually programming these days generally pays better than teaching programming.

  • 1
    Looking at open source projects is a great advice. Those two don't seem to be easy to follow if you're not already an expert IMO. – peval27 Mar 7 '17 at 20:14
  • Thats actualy a great idea! Thanks! :D Just need to find a class that extends one without parameters in it's constructor :) – PatriqDesigns Mar 7 '17 at 20:21
  • kevin cline, do you by any chance have an example? The source code is quite big so yeah :3 – PatriqDesigns Mar 7 '17 at 20:28
  • The source code for "STL" (you mean one of the various standard-library implementations, I suppose) or Boost serves a very different purpose than most other C++ code, because both are, by definition, general-purpose libraries or library collections which take genericity to the extreme; Boost also incluces thousands of hacks and workarounds for exotic and/or old compilers, with macros and proprietary stuff. Your own application code should never look like that. DeadMG's answer shows why the teacher's opinion should not so easily be dismissed as being "disconnected" from expert practice. – Christian Hackl Feb 13 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.