标签: overloading

是否可以使用string [] args和varargs重载主方法?如果不是那么为什么?

用string [] args和varargs重载主方法是否可能?如果没有那么为什么?我试过下面给出编译错误的代码.请帮助.im新增java.

public class obj1 {

    public static void main(String ... args) {
        System.out.println("main method varargs");
    }

    public static void main(String[] args) {
        System.out.println("main method string arrays");
    }
}
Run Code Online (Sandbox Code Playgroud)

java overloading

0
推荐指数
1
解决办法
334
查看次数

C ++ ifstream和ofstream重载运算符从文件中读取

大家好,我正在尝试重载ifstream和ofstream,但没有成功。

头文件:

#include <iostream>
#include <fstream>

using namespace std;

class Complex
{
    private:
        double real;
        double imaginary;
    public:
        //constructors
        Complex();
        Complex(double newreal, double newimaginary);
        ~Complex();
        //setter
        void setReal(double newreal);
        void setImaginary(double newimaginary);
        //getter
        double getReal();
        double getImaginary();
        //print
        void print();
        //methods
        Complex conjugate();
        Complex add(Complex c2);
        Complex subtraction(Complex c2);
        Complex division(Complex c2);
        Complex multiplication(Complex c2);

friend ifstream& operator >> (ifstream& in, Complex &c1)
{

    in >> c1;

    return in;
}

};
Run Code Online (Sandbox Code Playgroud)

测试文件:

#include <iostream>
#include <fstream>
#include <string>
#include "Complex2.h" …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

0
推荐指数
1
解决办法
5194
查看次数

方法重载int vs short

下面的类有重载方法calculate.第一种方法接受int,第二种方法接受short.

public class TestOverLoading
{
    public void calculate(int i)
    {
        System.out.println("int method called!");
    }

    public void calculate(short i)  //or byte
    {
        System.out.println("short method called!");
    }

    public static void main(String args[])
    {
        //Test1
        new TestOverLoading().calculate(5); //int method called

        //Test2         
        new TestOverLoading().calculate((short) 5); //short method called
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是如何int method called!打印Test1?它是如何确定5是intshort

java polymorphism overloading

0
推荐指数
1
解决办法
234
查看次数

故意隐藏重载功能警告避免

考虑以下C++示例main.cpp文件:

class FooIf
{
public:
    virtual int handle(char *req, char *res) = 0;
};

class BarIf
{
public:

    virtual void handle(char *msg) = 0;
};

class Bar : private BarIf
{
private:
    void handle(char * msg){}
};

class Zoo : public FooIf, public Bar
{
public:
    using FooIf::handle;
public:
    int handle(char *req, char *res){ return (0); }
};

int main(){

    Zoo zoo;
    return (0);
}
Run Code Online (Sandbox Code Playgroud)

我收到这个警告:

$ clang++ -ggdb -c main.cpp -Wall
main.cpp:23:6: warning: 'Zoo::handle' hides overloaded virtual …
Run Code Online (Sandbox Code Playgroud)

c++ hidden warnings overloading clang

0
推荐指数
1
解决办法
598
查看次数

如何编写C++模板函数来内部调用多个C函数?

我希望编写一个C++模板函数,该函数反过来使用一些"C"函数并利用函数重载.

例如,我需要myAbs使用模板编写一个函数,这些模板根据输入参数类型进行适当的调用fabsabs定义math.h.这该怎么做?

#include <math.h>
template<typename T>
T abs(T x)
{
   // I need to write an efficient code here!
   // If it is 'double' and 'float' I may be able to compare  the      
   // sizeof(Type) and call 'return fabs(x)' or 'return abs(x)'.
   // But this is not a good solution as two types can be of same size! 

}
Run Code Online (Sandbox Code Playgroud)

注意:我只是用它作为例子来解释我的问题.我已经知道这样的功能"abs"已经可用了<cmath>.

c c++ templates overloading

0
推荐指数
1
解决办法
112
查看次数

如何重载+ =运算符来返回一个字符串?

我只是有一个简单的问题:如何重载+ =运算符以返回一个字符串.这是我尝试过的,但没有成功.

// 'Student' is the class that this function is in
// 'get_name()' returns the name of the student
// 'get_grade()' returns the grade of the student
// Description:
// Ultimately I will be creating a list of students and their grades in
// the format of (Student1, Grade1) (Student2, Name2) ... (StudentN, GradeN)
// in a higher level class, and thus I need an overloaded += function.
Student& Student::operator+=(const Student& RHS)
{
    string temp_string;
    temp_string = "( " …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

0
推荐指数
1
解决办法
1249
查看次数

在“ std :: result_of”中没有名为“ type”的类型;从重载函数获取返回类型

我正在学习如何获得type重载函数test()vs 的返回值test(double)

我从SO答案(由chris)修改了代码。

#include <type_traits>
#include <utility>

int test();
double test(double x);

template<typename... Ts>
using TestType = decltype(test(std::declval<Ts>()...))(Ts...);

int main() {
    std::result_of< TestType<double> >::type n = 0;
     //^ ### compile error ###
    using doubleDat = std::result_of< TestType<double> >::type ;
    doubleDat n=0;
}
Run Code Online (Sandbox Code Playgroud)

我遇到了编译错误。

错误:“ std :: result_of”中没有名为“ type”的类型

我认为:-

  • TestType<...>是“可变模板”。
    用我自己的话说,它就像一个带有任何参数的压缩缩写

  • TestType<double>ID的的test(double)功能。

  • std::result_of<TestType<double>>::type是的返回类型test(double)
    doubleDat应该是double

问题: …

overloading result-of decltype variadic-templates c++11

0
推荐指数
1
解决办法
973
查看次数

为什么没有Stream.flatMap(Collection)方法?

目前,要将a转换List<List<Foo>>为a Stream<Foo>,您必须使用以下内容:

Stream<Foo> stream = list.stream().flatMap(fs -> fs.stream());
//or
Stream<Foo> stream = list.stream().flatMap(Collection::stream);
Run Code Online (Sandbox Code Playgroud)

我认为这正是方法引用的设计目标,它确实提高了可读性.现在考虑一下:

Stream<Bar> stream = list.stream().flatMap(fs -> fs.getBarList().stream());
Run Code Online (Sandbox Code Playgroud)

有两个链式方法调用,没有方法参考是可能的,我已经发生了几次这种情况.虽然这不是一个大问题,但它似乎偏离了方法参考简洁性.

在使用JavaFX 8之后,我注意到它们的API常量是方便的方法.Java是一种非常冗长的语言,在我看来,简单的方法重载是JavaFX的一大卖点.

所以我的问题是,我想知道为什么没有Stream.flatMap(Collection)可以被称为的方便方法:

Stream<Bar> stream = list.stream().flatMap(Foo::getBarList);
Run Code Online (Sandbox Code Playgroud)

这是甲骨文人员故意遗漏的吗?或者这会引起任何混淆吗?

注意:我知道"不基于意见的问题政策",我不是在寻找意见,我只是想知道是否有理由不采用这种方法.

java overloading java-8 java-stream

0
推荐指数
1
解决办法
133
查看次数

Java Wrapper类的'toString'方法是重载的还是一个抽象方法,它在不同的类中有不同的定义?

在下面的程序中,我试图使用Wrapper类的静态'toString'方法将Numbers转换为字符串.

class NumberToStringConversion
{
  public NumberToStringConversion()
 {
  String I=Integer.toSring(i);
  String F=Float.toString(f);
  String D=Double.toString(d);
  String L=Long.toString(l);
 }
}
Run Code Online (Sandbox Code Playgroud)

这里,Integer,Float,Double和Long是Wrapper类,它们分别包含各种Primitive数据类型.看起来该toString方法存在于所有上述包装类中.这是否意味着该toString方法过载?或者它是abstract一种在不同类别中具有不同定义的方法?

java overloading class wrapper

0
推荐指数
1
解决办法
277
查看次数

为什么SFINAE在这种情况下不起作用?

#include <iostream>
#include <type_traits>

template<typename T>
struct A
{
    using m = std::remove_pointer_t<T>&;
};

template
<
    typename T,
    typename = std::void_t<>
>
struct Test
{
    enum { value = 0 };
};

template<typename T>
struct Test<T, typename A<T>::m>
{
    enum { value = 1 };
};

int main()
{
    std::cout << Test<void*&>::value; // ok, output 0
    std::cout << Test<void*>::value; // error : cannot form a reference to 'void'
}
Run Code Online (Sandbox Code Playgroud)

第一种情况输出0,表示选择了主模板.所以,我认为第二种情况也应该选择主要模板而不是专用模板; 那么,应该没有错误.

预计Test<void*&>没关系; 让我感到惊讶的是Test<void*>应该不行!

为什么 …

c++ overloading sfinae type-traits c++17

0
推荐指数
1
解决办法
77
查看次数