overloading

C++ Base Class Function Overloading in a Subclass [duplicate]

萝らか妹 提交于 2021-02-05 08:34:48
问题 This question already has answers here : Why does an overridden function in the derived class hide other overloads of the base class? (4 answers) Closed 4 years ago . Given the following... #include <iostream> using namespace std; class BaseClass { public: void Func(float f) { cout << "BaseClass:Func() called!"; } }; class SubClass : public BaseClass { }; int main() { SubClass sub; sub.Func(1.1f); return 0; } This runs pretty much as one would expect, resulting in the following output...

Overloading a method which takes in Generic type causes ambiguous method call compile error

家住魔仙堡 提交于 2021-02-05 06:21:08
问题 See below simple snippet: public class GenericsOverloadingDistinguish<T> { public void print1(T t) { System.out.println("t"); } public void print1(Integer i) { System.out.println("integer"); } } public static void main(String[] args) { new GenericsOverloadingDistinguish<Integer>().print1(new Integer(1)); } This would cause an ambiguous method call and will not compile. This is utterly confusing on the user of that class. It is not able to call neither print1(T t) nor print1(Integer i) simple

How does const modifier for member functions affect overload resolution?

半世苍凉 提交于 2021-02-04 22:22:09
问题 I have the following test code: #include <string> #include <iostream> class CString { public: CString(char const*) {} }; class TestBed { public: void Comparison(CString const&) { std::cout << "CString Overload" << std::endl; } void Comparison(std::string const&) { std::cout << "std::string overload" << std::endl; } }; int main() { TestBed tb; tb.Comparison("Hello World"); } This code fails to compile because the call to Comparison() is ambiguous. I expect this behavior. However, when I make

Visual C++ methods in vfptr in reverse order

ぐ巨炮叔叔 提交于 2021-02-04 20:57:54
问题 Is there a way to control the order of some classes methods in the vfptr? It seems that Visual C++ 2010 at least puts the method pointers in the declaration order, except for overloaded methods. Below is sample code: enum ENUM { }; class CLASS { virtual void foo1() { }; virtual CLASS& __cdecl operator<<(const ENUM x) { return *this; }; virtual void foo2() { }; virtual CLASS& __cdecl operator<<(const char* x) { return *this; }; virtual CLASS& __cdecl operator<<(int x) { return *this; };

Call less constrained functionally equivalent function

安稳与你 提交于 2021-02-04 16:23:07
问题 Consider the following code: #include <iostream> #include <type_traits> struct A; template<class T> concept HasParent = std::is_convertible_v<typename T::parent*, A*>; struct A{}; struct B : A { using parent = A; }; template<class T> int foo(T*) { return 1; } template<HasParent T> int foo(T*) { // call the other one? return 2; } int main() { B b; std::cout << foo(&b) << std::endl; // displays 2 return 0; } Is it possible to call the general foo<T>(T*) function from foo<HasParent T>(T*) ?

Call less constrained functionally equivalent function

非 Y 不嫁゛ 提交于 2021-02-04 16:22:50
问题 Consider the following code: #include <iostream> #include <type_traits> struct A; template<class T> concept HasParent = std::is_convertible_v<typename T::parent*, A*>; struct A{}; struct B : A { using parent = A; }; template<class T> int foo(T*) { return 1; } template<HasParent T> int foo(T*) { // call the other one? return 2; } int main() { B b; std::cout << foo(&b) << std::endl; // displays 2 return 0; } Is it possible to call the general foo<T>(T*) function from foo<HasParent T>(T*) ?

Gcc Woverloaded-virtual error on PowerPC ppc64le

↘锁芯ラ 提交于 2021-01-29 14:38:57
问题 I am working on building Istio-envoy on rhel7.6:ppc64le . The build passes, however there are test failures: Error: In file included from test/server/filter_chain_benchmark_test.cc:19:0:` `bazel-out/ppc-fastbuild/bin/external/com_github_google_benchmark/_virtual_includes/benchmark/benchmark/benchmark.h:1071:16: error: 'virtual void benchmark::Fixture::SetUp(benchmark::State&)' was hidden [-Werror=overloaded-virtual]` `virtual void SetUp(State& st) { SetUp(const_cast<const State&>(st)); }` ` ^

Why can't the compiler select the correct String.contains method when using this lambda shorthand?

牧云@^-^@ 提交于 2021-01-29 14:30:00
问题 Say I want to check if a string contains any of the letters in "cory": def hasCory(input: String): Boolean = { val myName = "cory" input.exists(myName.contains) } The compiler complains with: error: type mismatch; found : CharSequence => Boolean required: Char => Boolean Scala provides the Char -accepting method I want in StringOps: But it appears that the compiler cannot see this method unless I change the code to one of: input.exists(myName.contains(_)) input.exists(c => myName.contains(c))

Why 'int' by default, but not 'byte'? [duplicate]

萝らか妹 提交于 2021-01-29 09:53:24
问题 This question already has answers here : How do you specify a byte literal in Java? (6 answers) Closed 9 months ago . Explain me please, why, when I write 4 overloaded methods and call it => it chooses method with 'int' as default, but not 'byte', which is closer/better, because it can storage values from -127 to 128? class Main { public static void method(short s) { System.out.println("short"); } public static void method(byte b) { System.out.println("byte"); } public static void method(int

Is the 'Overloads' keyword ever required in VB.Net?

假如想象 提交于 2021-01-29 04:57:34
问题 As the title says. From what I can see online the Overloads keyword is optional but is there ever a time when it is necessary? It even seems to be an error when used in a Module. 回答1: No, it's not neccessary. You can overload methods and properties without the Overloads keyword. However, if you use the Overloads or Overrides keyword on one overload of a method, you have to use it on all other overloads to that method in the class. You can use the Overloads keyword instead of the Shadows