overloading

call of overloaded ‘max(char&, char&)’ is ambiguous

∥☆過路亽.° 提交于 2019-12-10 19:36:06
问题 #include <iostream> using namespace std; int max (int a, int b) { return a<b?b:a; } template <typename T> T max (T a, T b) { return a<b?b:a; } template <typename T> T max (T a, T b, T c) { return max (max(a,b), c); } int main() { // The call with two chars work, flawlessly. :: max ('c', 'b'); // This call with three chars produce the error listed below: :: max ('c', 'b', 'a'); return 0; } Error: error: call of overloaded ‘max(char&, char&)’ is ambiguous Shouldn't this max ('c', 'b', 'a') call

Using a non static value as default argument in a function

混江龙づ霸主 提交于 2019-12-10 19:25:40
问题 Is there a nice way to have a non static value as default argument in a function? I've seen some older responses to the same question which always end up in explicitly writing out the overload. Is this still necessary in C++17? What I'd like to do is do something akin to class C { const int N; //Initialized in constructor void foo(int x = this->N){ //do something } } instead of having to write class C { const int N; //Initialized in constructor void foo(){ foo(N); } void foo(int x){ //do

C++ operator lookup misunderstanding

时光总嘲笑我的痴心妄想 提交于 2019-12-10 19:04:47
问题 I have a trouble with next case: template<typename T> void test(const T &ref){ cout << "By reference"; } template<typename T> void test(const T *ptr){ cout << "By pointer"; } Any parameter that I sent to the test() method will always pass to overloading with reference. Even this: int *p = 0; test(p); Can someone tell me why reference has so high priority and the place in standart where to read about this. Oh... I was inattentive! I have to specify both const and non-const overloading for a

Overloading member function among multiple base classes

扶醉桌前 提交于 2019-12-10 18:02:07
问题 Basically I want to have multiple member functions with same name, but different signature, spread in multiple base classes. Example: #include <iostream> struct A { void print(int) { std::cout << "Got an int!" << std::endl; } }; struct B { void print(double) { std::cout << "Got a double!" << std::endl; } }; struct C : A, B {}; int main() { C c; c.print((int)0); return 0; }; But I got this error on clang: main.cpp:18:7: error: member 'print' found in multiple base classes of different types c

Why is it OK to return an object reference inside a function in c++?

别来无恙 提交于 2019-12-10 17:57:06
问题 Here is an example from website: http://www.cplusplus.com/doc/tutorial/classes2/ I know it is a working example. However, I don't understand why object temp can be returned from the operator+ overloading function. I have made some comments besides the codes. // vectors: overloading operators example #include <iostream> using namespace std; class CVector { public: int x,y; CVector () {}; CVector (int,int); CVector operator + (CVector); }; CVector::CVector (int a, int b) { x = a; y = b; }

Overloaded method calling overloaded method

痞子三分冷 提交于 2019-12-10 17:52:49
问题 I have a method that I'm writing that is calling another overloaded method inside it. I'd like to only write one outer method, since the parameter to the outer method is being passed to the inner one. Is there a way to do this? I tried using generics, but I don't know enough about this so it isn't working: public void OuterMethod<T>(T parameter) { InnerMethod(parameter); // InnerMethod accepts an int or a string } I know that I can do this: public void OuterMethod(string parameter) {

overloading Ruby's […] Array creation shorthand

混江龙づ霸主 提交于 2019-12-10 17:25:14
问题 I've written a library that extends several base Ruby classes with observing wrappers mostly through method aliasing. However, I've hit a roadblock with the Array instantiation shorthand (e.g. @a = [1, 2, 3] ) I can't seem to find any method that's actually called in the creation of an Array object by the shorthand means. It's not an inherited #[] method in the current scope or inherited from any class or module in the ancestors chain. I've also overloaded or watched every method from the

What operator do I overload when assigning an “Enhanced Record” to a normal “Data Type” variable?

 ̄綄美尐妖づ 提交于 2019-12-10 17:12:34
问题 I need to know, first and foremost, if what I'm trying to do is even possible. If it is possible, I then need to know how. It's far easier to demonstrate the problem rather than explain it so here goes: I have an "Enhanced Record" (the purpose - though not important to this question - is to produce a "Smart String" type, to replace the normal String type): TLKString = record Value: String; // Some methods here to operate on and build String values // Allows me to assign String values directly

How does polymorph ambiguity distinction work?

两盒软妹~` 提交于 2019-12-10 17:04:52
问题 Given I have a class with two constructors: public class TestClass { ObjectOne o1; ObjectTwo o2; public TestClass(ObjectOne o1) { // .. } public TestClass(ObjectTwo o2) { // .. } } Please assume, that ObjectOne is an interface type, and ObjectTwo implements ObjectOne . What happens, if I call: new TestClass(null); How to determine the correct method to call? And who determines that? Are there differences between Java and other OOP languages? 回答1: This question is really about resolving

Method overloading

心不动则不痛 提交于 2019-12-10 16:24:05
问题 I was wondering if you can suggest something here. I would like to have 2 methods: doSomething(List<Data>) and doSomething(List<Double>) Since type of parameter is the same, Java is complaining Is there a way to somehow make this overloading happen? 回答1: public void doSomething(List list) { if(list.size() > 0) { Object obj = list.get(0); if(obj instanceof Data) { doSomethingData((List<Data>)list); } else if (obj instanceof Double) { doSomethingDouble((List<Double>)list); } } } 回答2: Sadly, no.