4

I've been asked to name three things that can not be inherited from the base class.

Apart from private member functions, what else can I add?

I thought about friend functions but since they don't actually belong to the class, they have nothing to do with inheritance.

Bart
  • 19,692
  • 7
  • 68
  • 77
Max
  • 3,824
  • 8
  • 41
  • 62
  • 5
    "private functions with names starting with 'n'", "private functions with names starting with 'o'", and "private functions with names not starting with 'n' or 'o'" :P – R. Martinho Fernandes Jun 08 '12 at 04:55
  • @R.MartinhoFernandes, can you elaborate more ? lolz – Amit Jun 08 '12 at 04:57
  • @R.MartinhoFernandes: Could'nt quite understand... lolz.. :) – verisimilitude Jun 08 '12 at 04:58
  • I think constructors are destructors are also not inherited ... is that the reason we need to call base class's version separately ??? – Amit Jun 08 '12 at 04:59
  • @anDroider Constructors and destructors can be inherited. – Max Jun 08 '12 at 05:02
  • @crazyfffan I don't think you're right. http://stackoverflow.com/questions/347358/inheriting-constructors –  Jun 08 '12 at 05:42
  • You mention friend functions. It might be worth noting that *friendships* are not inherited -- that is to say, if I make `Base` a friend of my class, it doesn't follow that `Derived` is also a friend. Whether that's an answer to the question depends whether you consider friendships a "thing" :-) – Steve Jessop Jun 08 '12 at 09:54

2 Answers2

8

A few obvious ones you usually care about are constructors, assignment operators and destructors.

In all these cases, a new version specific to the derived class is either provided by the user, or else synthesized by the compiler (though C++11 also adds some capabilities for things like simply deleting one that you don't want available).

I should probably add that "can not be inherited" isn't necessarily exactly correct. For example, C++11 adds inheriting constructors (but they weren't in C++98/03, which is what most courses still deal with). Even in C++11, you don't inherit them by default.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Private member variables, and private bases. You also cannot inherit template arguments, COM __uuids, and whether or not the class is exported from a DLL.

Assignment operators cannot be inherited.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 3
    I think you're wrong, private member variables are inherited, but derived classes cannot access them directly. – Max Jun 08 '12 at 05:04
  • @crazyffan: So just add "Ability to access" in front. Besides, I provided more than that.# – Puppy Jun 08 '12 at 05:11
  • Thanks for the "more than that" but `template arguments, COM __uuids` are out of scope since this question is for first year students, I believe the answer must be much simpler. – Max Jun 08 '12 at 05:15
  • 1
    @crazyfffan: Well, I would think so, but only an idiot lecturer would ask such a question, so who knows? – Puppy Jun 08 '12 at 05:18
  • @crazyfffan: Also, you failed to state the "idiot lecturer" requirement in your question. – Puppy Jun 08 '12 at 05:29
  • *Assignment operators **are** inherited* but just hidden because derived class assignment operator hides the base class operator. – Alok Save Jun 08 '12 at 05:33
  • @Als: No, the compiler-defined derived class operator calls the base class operator. If you do not define your own and violate the requirements for a compiler-defined one, then none shall exist. – Puppy Jun 08 '12 at 05:37
  • *"the compiler-defined derived class operator calls the base class operator"*, doesn't that actually say the assignment operator is inherited, The derived class implicit operator won't be able to call the Base class operator if there was no inheritance. – Alok Save Jun 08 '12 at 05:41
  • @Als: No. If the Base class operator was inherited, it would be available on the Derived class public interface. It is not. – Puppy Jun 08 '12 at 05:56
  • private members are their in the derived class, but they are not visible... check the sizeof of values of both objects... – Amit Jun 08 '12 at 05:58
  • @DeadMG: "If the Base class operator was inherited, it would be available on the Derived class public interface" -- by this definition, `protected` members are not inherited. Your answer (perhaps deliberately) shows that there's some room for interpretation what "inherited" actually means. For another example of a way to subvert what is probably meant by "not inherited", the mangled name of the class is not inherited, since derived classes have different manged names from their bases :-) – Steve Jessop Jun 08 '12 at 08:56
  • 2
    @DeadMG: also, I don't think it's reasonable to call the lecturer an idiot. No doubt the lecture (or other course materials) about inheritance included examples of things that are inherited, and things that are not. The purpose of the question is to see whether the student has learned the course materials, no? It's pretty common for programmers to insult educators, on the basis that educators do things other than write profit-making code, but I think that's misguided. Better-guided would be to insult educators only if their students emerge unable to write code. Which is frequent! – Steve Jessop Jun 08 '12 at 09:02
  • @SteveJessop: I call lecturers idiots for making their students write C-string `new[]` code, and this is no different. When was the last time I dealt with this topic in actual code? Well, never, and I don't know anyone else who did, else they'd post better answers. It's a waste of the student's time. – Puppy Jun 08 '12 at 09:33
  • 2
    @DeadMG: that's exactly what I mean, criticising educators for doing anything other than writing "actual code". You think it's a waste of students' time to be given examples of things that are or are not inherited. I don't really see how else you could competently teach somebody what inheritance in C++ does. Read out all the relevant passages from the standard, I suppose. The reason I didn't answer isn't that I've never needed to know any of this, it's that I think Jerry's answer is fine, and yours is amusing. I don't have more examples to add. – Steve Jessop Jun 08 '12 at 09:49