overloading

Can we overload main() function in C++? [duplicate]

江枫思渺然 提交于 2019-12-30 16:39:09
问题 This question already has answers here : Is main() overloaded in C++? (6 answers) Closed 2 years ago . Since C+++ allows function overloading, can we overload main() ? For example, int main(const std::string &) { return 0; } int main(int argc, char *argv[]) { return main("calling overloaded main"); } gcc-4.3.4 doesn't compile this, and gives these errors: (see at ideone) prog.cpp:4: error: first argument of ‘int main(const std::string&)’ should be ‘int’ prog.cpp:4: error: ‘int main(const std:

Overloaded method selection logic

六月ゝ 毕业季﹏ 提交于 2019-12-30 10:18:07
问题 Given the following overloaded methods: public string Test(long item) { return "Test with a long was called!"; } public string Test(int item) { return "Test with an int was called!"; } public string Test(object item) { return "Test with an object was called!"; } When I call Test() , passing a short , like this: short shortValue = 7; var result = Test(shortValue); Why is the value result equal to "Test with an int was called!" , instead of "Test with an object was called!" ? 回答1: Why is the

Generic contraints on method overloads

半腔热情 提交于 2019-12-30 09:36:16
问题 I've got an interface with some generic methods, and I wanted to implement a method with overloads to either accept an instance of a class, or its PK value (which is either an int or GUID but does vary). I added to methods similar to these examples: void DoSomething<TKey>(TKey key) where TKey: struct; void DoSomething<TModel>(TModel model) where TModel : class; The 'DoSomething' method name on the second of these is highlighted, and the error is Type 'ISomeStuff' already defines a member

What does the C++ compiler do when coming ambiguous default parameters?

こ雲淡風輕ζ 提交于 2019-12-30 09:36:14
问题 What does the C++ compiler do when coming ambiguous default parameters? For example, let's say there was a function such as: void function(int a = 0, float b = 3.1); void function(int a, float b =1.1, int c = 0); Is the above considered ambiguous? If not, what does the compiler do (how is the function matched exactly) when calling something like function1(10) ? Thanks! 回答1: The following is fine void function(int a = 0, float b = 3.1); void function(int a, float b =1.1, int c = 0); And the

Ambiguous call to overloaded function - std::to_string [duplicate]

为君一笑 提交于 2019-12-30 08:03:16
问题 This question already has answers here : std::to_string - more than instance of overloaded function matches the argument list (2 answers) Closed 6 years ago . In attempting to insert integer values into a string, I thought that my prayers were answered when I found std::to_string, but for some reason whenever I actually try to use it, Visual Studio complains about ambiguity. Here is the current incarnation of my function: string get_time_remaining (int elapsed) { string remaining; string temp

overloading vs overriding

允我心安 提交于 2019-12-30 04:06:04
问题 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

How to explain this behaviour with Overloaded and Overridden Methods? [duplicate]

佐手、 提交于 2019-12-30 03:03:56
问题 This question already has answers here : Overload resolution and virtual methods (5 answers) Closed 5 years ago . Could anyone be so nice and explain me why this code shows Derived.DoWork(double) . I can come up with some explanations for this behaviour, however I want someone to clarify this for me. using System; public class Base { public virtual void DoWork(int param) { Console.WriteLine("Base.DoWork"); } } public class Derived : Base { public override void DoWork(int param) { Console

How to explain this behaviour with Overloaded and Overridden Methods? [duplicate]

China☆狼群 提交于 2019-12-30 03:03:14
问题 This question already has answers here : Overload resolution and virtual methods (5 answers) Closed 5 years ago . Could anyone be so nice and explain me why this code shows Derived.DoWork(double) . I can come up with some explanations for this behaviour, however I want someone to clarify this for me. using System; public class Base { public virtual void DoWork(int param) { Console.WriteLine("Base.DoWork"); } } public class Derived : Base { public override void DoWork(int param) { Console

volatile overloading?

五迷三道 提交于 2019-12-30 01:12:21
问题 I heard that volatile is factor of overloading like const. If a function is overloaded by volatile parameter, when is the volatile-version called? I can't imagine a situation when the volatile-version is called. 回答1: Volatile can be applied to parameters, but it is not a factor of overloading when applied directly to the parameter. It is however possible to use it to distinguish types of the parameter. For example, this is legal: void f(int &p) {}; //reference to int void f(volatile int &p) {

What's going on with overriding and overloading here in C++?

给你一囗甜甜゛ 提交于 2019-12-29 07:52:28
问题 This doesn't work: class Foo { public: virtual int A(int); virtual int A(int,int); }; class Bar : public Foo { public: virtual int A(int); }; Bar b; int main() { b.A(0,0); } It seems that by overriding Foo::A(int) with Bar::A(int) I have somehow hidden Foo::A(int,int) . If I add a Bar::A(int,int) things work. Does anyone have a link to a good description of what's going on here? 回答1: Essentially, name lookup happens before overload resolution so the function A in your derived class overrides