overloading

overloading vs overriding

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 12:33:36
I am a little confused over the two terminologies and would be glad to get some doubts clarified. As I understand function overloading means having multiple methods in the same class with same name but either with different number of arguments, different types of arguments or sequence of arguments irrespective of the return type which has no effect in mangled name of the functions. Does the above definition also include "....in the same class or across related classes(related through inheritance)....." And Function Overriding is related to virtual functions, same method signature(declared

implicit instantiation of undefined template 'class'

独自空忆成欢 提交于 2019-11-30 12:20:36
When trying to offer functions for const and non-const template arguments in my library I came across a strange problem. The following source code is a minimal example phenomenon: #include <iostream> template<typename some_type> struct some_meta_class; template<> struct some_meta_class<int> { typedef void type; }; template<typename some_type> struct return_type { typedef typename some_meta_class< some_type >::type test; typedef void type; }; template<typename type> typename return_type<type>::type foo( type & in ) { std::cout << "non-const" << std::endl; } template<typename type> void foo(

Overloading structs with template call operator and generic lambdas - gcc vs clang

懵懂的女人 提交于 2019-11-30 12:17:45
I have discovered a code snippet that compiles and works properly in clang++ 4 (and trunk) but fails to compile in g++ 7 (and trunk) . Let's assume I have the following struct types: struct a { void foo() { } }; struct b { void bar() { } }; struct c { void bar() { } }; I want to create an overload set out of lambdas which handles a explicitly, while b and c are "caught" with a generic lambda using an auto parameter: auto ol = overload([](a x) { x.foo(); }, [](auto x){ x.bar(); }) When I invoke ol(a{}) : clang++ compiles and behaves as expected: a "matches" the first lambda, while b and c match

Polymorphism and method overloading

≯℡__Kan透↙ 提交于 2019-11-30 11:51:22
问题 I have a quick and straighforward question: I have this simple class: public class A { public void m(Object o) { System.out.println("m with Object called"); } public void m(Number n) { System.out.println("m with Number called"); } public static void main(String[] args) { A a = new A(); // why will m(Number) be called? a.m(null); } } UPDATE: actually is method with Number actually being called. Sorry about the confusion. If I call a.m(null) it calls method with Number parameter. My question is

Question about Java overloading & dynamic binding

亡梦爱人 提交于 2019-11-30 11:47:20
问题 In the code below, how does first and second print statements print out SubObj?? Do top and sub point to the same Sub class? class Top { public String f(Object o) {return "Top";} } class Sub extends Top { public String f(String s) {return "Sub";} public String f(Object o) {return "SubObj";} } public class Test { public static void main(String[] args) { Sub sub = new Sub(); Top top = sub; String str = "Something"; Object obj = str; System.out.println(top.f(obj)); System.out.println(top.f(str))

Overloads of inherited member functions

Deadly 提交于 2019-11-30 11:36:25
Can a class overload methods that also exist in the publicly inherited interface? It seems like this is unambiguous and useful, but compilers (VC, Intel, GCC) all complain, at least by my construction. Below is a toy example. The inherited rebound() function has two clear overloads, yet this will not compile. If you rename the rebound() method in either class, it works fine, but if they share the same member function name (even though they're overloaded with different argument types!) you get a fatal error of "too few arguments to function call." The workaround is trivial (I'll just rename the

Choose function to apply based on the validity of an expression

天大地大妈咪最大 提交于 2019-11-30 11:33:42
The problem is the following, in C++14 : Let's have two functions FV&& valid_f , FI&& invalid_f , and arguments Args&&... args The function apply_on_validity should apply valid_f on args if the expression std::forward<FV>(valid_f)(std::forward<Args>(args)...) is valid Otherwise and if std::forward<FV>(invalid_f)(std::forward<Args>(args)...) is a valid expression, apply_on_validity should apply invalid_f on args Otherwise apply_on_validity should do nothing I guess the code should look like something like this: template <class FV, class FI, class... Args, /* Some template metaprog here */> void

Why is ::operator new[] necessary when ::operator new is enough?

时光毁灭记忆、已成空白 提交于 2019-11-30 11:30:59
问题 As we know, the C++ standard defines two forms of global allocation functions: void* operator new(size_t); void* operator new[](size_t); And also, the draft C++ standard (18.6.1.2 n3797) says: 227) It is not the direct responsibility of operator new or operator delete to note the repetition count or element size of the array. Those operations are performed elsewhere in the array new and delete expressions. The array new expression, may, however, increase the size argument to operator new to

Why is this Java method call considered ambiguous?

青春壹個敷衍的年華 提交于 2019-11-30 11:22:33
I've come across a strange error message that I believe may be incorrect. Consider the following code: public class Overloaded { public interface Supplier { int get(); } public interface Processor { String process(String s); } public static void load(Supplier s) {} public static void load(Processor p) {} public static int genuinelyAmbiguous() { return 4; } public static String genuinelyAmbiguous(String s) { return "string"; } public static int notAmbiguous() { return 4; } public static String notAmbiguous(int x, int y) { return "string"; } public static int strangelyAmbiguous() { return 4; }

Overload the behavior of count() when called on certain objects [duplicate]

ぐ巨炮叔叔 提交于 2019-11-30 11:17:25
Possible Duplicate: Count elements for objects implementing ArrayAccess using count()? In PHP 5, you can use magic methods, overload some classes, etc. In C++, you can implement functions that exist as long as the argument types are different. Is there a way to do this in PHP? An example of what I'd like to do is this: class a { function a() { $this->list = array("1", "2"); } } $blah = new a(); count($blah); I would like blah to return 2. IE count the values of a specific array in the class. So in C++, the way I would do this might look like this: int count(a varName) { return count(varName-