标签: overloading

如何在同一个函数中传递不同数量的参数?

我有类似的问题,但没有得到满意的答案.

我有一个功能

func(int a,int b)
{
//code....
}
Run Code Online (Sandbox Code Playgroud)

该函数接受两个参数(a和b).我想在同一个函数中传递不同数量的参数.我知道有一个重载的概念,但我不知道我将通过多少个参数.我在C#(asp.net)工作.

c# overloading function

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

Java:抽象泛型类和方法重载顺序

看看这堂课

abstract class LolCat<T> {
    T execute() throws Err, Arr {
        T lol = doSomething();
        if (lol == null)
            lol = doSomething();

        return lol;
     }

    abstract T doSomething();
    abstract T doSomething() throws Err, Arr;
}
Run Code Online (Sandbox Code Playgroud)

现在我们在LolCat的某些方法中有一个匿名实现,就像这样

final UhmLetsSayCat cat = new ImplLolCat<UhmLetsSayCat>() {
    @Override
    UhmLetsSayCat doSomething() {
        return null; // somehow a UhmLetsSayCat is returned for real sometimes null
    }

    @Override
    UhmLetsSayCat doSomething() throws Err,Arr {
        return null; // really it does right thing, whatever
    }
 }.execute();
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,为什么第一次调用doSomething()会转到不抛出Err,Arr但第二次调用的方法,如果lol为null,则运行抛出Err,Arr的doSomething实现.如何区分运行哪些方法!?

编辑:错误报警.我的天啊.我一定是整个晚上一直都是瞎​​子,做某事实际上是不一样的方法名称,他们叫做querryAll和QuerryAl1,我会找到那个做过它并揍他的人.

EDIT2:谢谢大家.

java generics abstract-class overloading

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

重载all-const类型的复制赋值运算符的正确方法是什么?

假设我有这样的结构:

struct Foo
{
  const int bar;
  const char baz;

  Foo& operator=(const Foo& other)
  {
    memcpy(this,&other,sizeof(Foo)); //How am I supposed to write this decently?
    return *this;
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望Foo的所有字段都是final,我希望Foo类型的变量与其他原始值类型一样.说,int,当然我们可以写这样的东西:

 int i = 0;
 i = 42;

 Foo foo = {007,'B'}
 foo = {42,'X'}
Run Code Online (Sandbox Code Playgroud)

然而,对于我可怜的Foo类型,我是否必须使用memcpy这样的方法来解决类型安全检查?我知道我可以删除const修饰符,将字段标记为私有并添加一些getter,但这不是重点.我只想知道是否有一种不错的方式来编写=运算符的内容.

提前致谢!

~~~~~

查看以下示例:

//If the = op is not implemented, this won't compile
Foo stat;
for(int i=0;i!=100;++i)
{
  stat = func(i);
  if(stat.bar == 0)...
}

//But weird thing is, if I declare the 'stat' inside the …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

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

在Haskell中进行JSON解析期间的重载分辨率

我正在阅读"真实世界Haskell"(好书),我对编译器如何选择重载函数感到困惑.

如果我有一个类型类

type JSONError = String

class JSON a where
    toJValue :: a -> JValue
    fromJValue :: JValue -> Either JSONError a
Run Code Online (Sandbox Code Playgroud)

和这样的两个实例

instance JSON Bool where
    toJValue = JBool
    fromJValue (JBool b) = Right b
    fromJValue _ = Left "not a JSON boolean"
Run Code Online (Sandbox Code Playgroud)

instance JSON String where
    toJValue = JString
    fromJValue (JString s) = Right s
    fromJValue _ = Left "not a JSON string"
Run Code Online (Sandbox Code Playgroud)

编译器如何在两个"fromJValue"函数之间进行选择,例如,给出一个Integer?

parsing haskell overloading typeclass

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

函数重载,无法减少模板参数

我正在尝试重载一个Sum函数,该函数接受[list或vector] start和end迭代器作为参数.这个编译错误让我很困惑.相关代码如下:

template <typename T1, typename T2>
const double Sum(const typename T1::const_iterator& start_iter, const typename T2::const_iterator& end_iter)
{// overloaded function that calculates sum between two iterators
    typename T1::const_iterator iterator_begin = start_iter;
    typename T2::const_iterator iterator_end = end_iter;

    double my_sum = 0;

    for (iterator_begin; iterator_begin != iterator_end; iterator_begin++)
        my_sum += *iterator_begin;

    return my_sum;      
}

int main()
{

list<double> test_list(10,5.1);
cout << Sum(test_list.begin(), test_list.end()); // compiler errors here

}
Run Code Online (Sandbox Code Playgroud)

我得到以下编译器错误:

iterators.cpp(72):错误C2783:'const double Sum(const T1 :: const_iterator&,const T2 :: const_iterator&)':无法推导'T1'的模板参数

iterators.cpp(72):错误C2783:'const double Sum(const T1 …

c++ templates iterator stl overloading

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

为什么在这个例子中,按值传递有效?

这个例子取自Java编程书; 第4版.

在绊倒这个代码示例后,我很困惑,为什么它仍然打印出对象的新引用,即使我们声明它为'null'.理论上,我们正在改变对整个程序共享的对象的引用,尽管我们在commonName方法中将对象初始化为null.在commonName的控制流程点,body构造函数中的字段被初始化为"Sirius"; 当我们改变对象的引用时(在Java中,你可以按值调用),该字段将更改为Dog Star.我们将整个对象设置为null的方法的最后一行,一旦我们打印出对象,运行时应该使用空引用来问候我们.

解决这个问题的唯一方法是将commonName方法设置为final.任何Java专家都可以解释为什么会发生这种情况,特别是在任何按值调用的语言中.

class PassRef
{
    public static void main (String[] args) {
        Body sirius = new Body ("Sirus", null);
        System.out.println ("before: " + sirius);
        commonName(sirius);
        System.out.println("after: " + sirius);
    }

    public static void commonName (Body bodyref) {
        bodyref.name = "Dog Star";
        bodyref = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

before: 0 (Sirius)
after:  0 (Dog Star)
Run Code Online (Sandbox Code Playgroud)

java overloading pass-by-value

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

为什么"ostream&os"必须存在于C++运算符重载?

// The original code is from link: http://hoeven.blogbus.com/logs/37324287.html
#include<iostream>
#include<vector>
#include <stdlib.h>

using namespace std;

class test{
public:
     int v;
   /*????*/
     test():v(0){}
     test(const int &a):v(a){}
     test(const test &t1):v(t1.v){}

   /*??????? < */
     //?????????
     bool operator<(const test &t1) const{
         return (v < t1.v);
     }
     //?????int???
     bool operator<(const int &t1) const{
         return (v < t1);
     }
     //???????int??????
     friend inline bool operator<(const int &a, const test & t1){
         return (a < t1.v);
     }

   /*??????? = */
     //?????
     test & operator=(const test &t1){
         v = …
Run Code Online (Sandbox Code Playgroud)

c++ overloading

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

重载的一元运算符++需要一个参数

我想在C#中重载运算符'++',但是当我编写下面的代码时,VS 2012会给我一个错误消息.

    public LogItem operator ++()
    {
        ++ visitTimes;
    }
Run Code Online (Sandbox Code Playgroud)

错误是重载的一元运算符++需要一个参数

我在这里是LogItem 的定义:

     public class LogItem
     {
        /**
         * Constructor
         */
        public LogItem(string ip)
        {
            ipAddress = ip;
            visitTimes = 0;
        }

        /**
         * Operator Overload Function
         */
        public LogItem operator ++() 
        {
            ++ visitTimes;
        }

        public string ipAddress { get; private set; }
        public string location { get; set; }
        public int visitTimes { get; private set; }
     }
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能重载运算符'++'?

c# overloading operator-keyword

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

重载赋值运算符以指向两个不同的类

我的问题:我正在尝试重写赋值运算符以指向两个不同的类.这是一个例子:

dc.h:

#ifndef DC_H_
#define DC_H_

#include "ic.h"

class dc {

double d;
char c;

public:

dc(): d(0), c(0) { }

dc(double d_, char c_): d(d_), c(c_) { }

dc* operator=(const ic* rhs);

~dc() { }

};

#endif /* DC_H_ */
Run Code Online (Sandbox Code Playgroud)

class ic.h:

#ifndef IC_H_
#define IC_H_

class ic {

int i;
char c;

public:

ic(): i(0), c(0) { }

ic(int i_, char c_): i(i_), c(c_) { }

~ic() { }

};

#endif /* IC_H_ */
Run Code Online (Sandbox Code Playgroud)

dc.cpp:

#include "dc.h"

dc* …
Run Code Online (Sandbox Code Playgroud)

c++ overloading variable-assignment operator-keyword

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

错误c6277在C++中重载&&

在我班上我有成员职能:

const bool operator&&(const KinematicVariable &right) const { 
        return this->isUsed() && right.isUsed(); 
}
inline const bool isUsed() const { return this->_used; }
Run Code Online (Sandbox Code Playgroud)

然后我试试

if (k1 && k2 && k3)
Run Code Online (Sandbox Code Playgroud)

但我明白了

error: C2677: binary '&&' : no global operator found which takes type 
'KinematicVariable' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

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