Override and overload in C++

Overloading generally means that you have two or more functions in the same scope having the same name. The function that better matches the arguments when a call is made wins and is called. Important to note, as opposed to calling a virtual function, is that the function that’s called is selected at compile time. … Read more

Public operator new, private operator delete: getting C2248 “can not access private member” when using new

When you do new Foo() then two things happen: First operator new is invoked to allocate memory, then a constructor for Foo is called. If that constructor throws, since you cannot access the memory already allocated, the C++ runtime will take care of it by passing it to the appropriate operator delete. That’s why you … Read more

overloaded functions are hidden in derived class

TTBOMK this doesn’t have a real technical reason, it’s just that Stroustrup, when creating the language, considered this to be the better default. (In this it’s similar to the rule that rvalues do not implicitly bind to non-const references.) You can easily work around it be explicitly bringing base class versions into the derived class’ … Read more

Calling base class overridden function from base class method

Unfortunately, no As i’m sure you’re aware, but I’ll state explicitly for completeness – there are only the 2 keywords to control the method invocation: this – this.method() – looks for method starting from the invoking instance’s class (the instance’s “top” virtual table – implied default) super – super.method() – looks for method starting from … Read more

How can I create a Java method that accepts a variable number of arguments?

You could write a convenience method: public PrintStream print(String format, Object… arguments) { return System.out.format(format, arguments); } But as you can see, you’ve simply just renamed format (or printf). Here’s how you could use it: private void printScores(Player… players) { for (int i = 0; i < players.length; ++i) { Player player = players[i]; String … Read more