overloading

Calling overrided virtual function instead of overloaded

北城余情 提交于 2020-01-05 02:46:07
问题 Say i have this part of code: #include<iostream> using namespace std; class A { public: virtual int f(const A& other) const { return 1; } }; class B : public A { public: int f(const A& other) const { return 2; } virtual int f(const B& other) const { return 3; } }; void go(const A& a, const A& a1, const B& b) { cout << a1.f(a) << endl; //Prints 2 cout << a1.f(a1) << endl; //Prints 2 cout << a1.f(b) << endl; //Prints 2 } int main() { go(A(), B(), B()); system("pause"); return 0; } I can

Error CS1501: I'm not overloading a Sum() method correctly

China☆狼群 提交于 2020-01-05 02:03:31
问题 Below is draft number 5 for my C# Homework this week. I wrote the program out using Linq first, and it worked fine. Unfortunately, the directions state that I must create my own method instead of using the wonderful Sum() method already found in Linq. The major problem with this source code is that the method overload is incorrect (and it's also probable that my entire Sum() method is wrong too). Since our almighty text doesn't clearly explain how to overload a method like this, I'm kind of

Overloading the << operator

喜夏-厌秋 提交于 2020-01-04 18:22:29
问题 I am trying to overload the << operator. I have successfully overload the other operators but this last one is giving me trouble. Maybe it just needs a new set of eyes. I have a feeling it is all being caused by a const qualifier. Where I need the operator to work. for(UINT i = 0; i < setVector.size(); ++i) { outStream << "SET " << i << setVector[i] << endl; } where setVector is a vector of type Set. Set is a vector of int . This is my operators.cpp /******************************************

Overloading the << operator

那年仲夏 提交于 2020-01-04 18:19:06
问题 I am trying to overload the << operator. I have successfully overload the other operators but this last one is giving me trouble. Maybe it just needs a new set of eyes. I have a feeling it is all being caused by a const qualifier. Where I need the operator to work. for(UINT i = 0; i < setVector.size(); ++i) { outStream << "SET " << i << setVector[i] << endl; } where setVector is a vector of type Set. Set is a vector of int . This is my operators.cpp /******************************************

Why does operator>> (or <<) overloading function need to receive an i\ostream reference?

非 Y 不嫁゛ 提交于 2020-01-04 15:17:11
问题 From cplusplus.com, I saw that ostream class's member function operator<< looks like this: ostream& operator<< (bool val); ostream& operator<< (int val); .... and so on. It does make sense because when you use the Cout object like cout<<x you activate the ostream& operator<< (int val) function, so you actually use the << operator on Cout object. This is very much like every other operator and sends the int variable to the function. What is the difference and what exactly happens when I want

Benefits of having both specific arguments and params method overloads in C#

心不动则不痛 提交于 2020-01-04 04:21:10
问题 There are a number of examples in the .NET framework where there are multiple overloads for a method, some of which use a specific number of parameters followed by a final "catch all" where the params keyword is used. Common examples of this are on the String class e.g.: String.Format() String.Concat() I was wondering if there is a particular reason for why there are so many of these method overloads? At first I thought it might be something to do with performance; the question and answers to

Dynamic Casts or Function Overloads?

こ雲淡風輕ζ 提交于 2020-01-04 03:53:25
问题 Consider the following abstract class: class Abstract { public: // ... virtual bool operator==(const Abstract& rhs) const = 0; // ... }; Now suppose I'm creating multiple derived classes from this abstract class. However, each one uses a different algorithm when comparing with its own type, and a generic algorithm when comparing with any of the other derived classes. Between the following two options, which would be the better, more efficient option? Option A: class Derived : public Abstract

C++ Function Overloading Similar Conversions

守給你的承諾、 提交于 2020-01-04 02:54:33
问题 I'm getting an error which says that two overloads have similar conversions. I tried too many things but none helped. Here is that piece of code CString GetInput(int numberOfInput, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT); CString GetInput(int numberOfInput, string szTerminationPattern, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT); I can't understand how could string be equal to long ? I'm using Visual C++ 6 (yep I know its old, I'm working on legacy code, so I'm

overloading a float to a numpy array

自闭症网瘾萝莉.ら 提交于 2020-01-04 02:51:11
问题 I have a function, processing a 1D numpy array, like this: def f(arr): arr=asarray(arr) #process data as numpy array #... return arr With asarray I allow to call the function with a list as f([4,5,6]) . Now, I would like to "overload" the argument also to a single float, so that I can use f(4) instead of f([4]) . This is a standard numpy feature, since you can call np.sin as sin(array([4,5,6])) , or as sin([4,5,6]) or as sin(4) as well. I came up with this code, that works at least in simple

Inconsistent behavior of builtin when overloading built-in functions in Octave

寵の児 提交于 2020-01-04 02:42:08
问题 I am trying to overload some built-in functions in Octave to perform a custom action prior to calling the built-in version of the overloaded function. In MATLAB (and supposedly Octave), I can do this using the builtin function. The typical function definition would look something like this where I forward all inputs/outputs to/from the built-in after doing my custom action: function varargout = disp(varargin) % Do a custom thing fprintf('Calling overloaded disp!\n') % Now call the builtin