有没有办法在 C# 中重载指数?我知道在某些语言中(无法说出任何名字),^用于指数函数,但在 C++ 和 C# 中,^是按位异或运算符,可以重载,但这不是我想要的超载。我不想超载电源功能或其他任何东西
例如。我知道我可以2^x用(1 << x). 所以,我的第二部分,在 C# 中有一个指数运算符还是我必须坚持使用System.Math.Pow(base, exp);
可能的重复:
C++ 中的切片问题是什么?
我有一个简单的代码作为多态和继承的例子
class A
{
public:
int fieldInA;
void virtual overloadedFunc()
{
printf("You are in A\n");
}
};
class B : public A
{
public:
int fieldInB;
void overloadedFunc()
{
printf("You are in B\n");
}
};
void DoSmth(A* a)
{
a->overloadedFunc();
}
void DoSmthElse(A a)
{
a.overloadedFunc();
}
int _tmain(int argc, _TCHAR* argv[])
{
B *b1 = new B();
B b2;
//Breakpoint here
DoSmth(b1);
DoSmthElse(b2);
scanf("%*s");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我停在断点处时,b1 的 _vfptr[0] 和 b2 的 _vfptr[0] 的值是相同的(SomeAddr(B::overloadedFunc(void)))。在 DoSmth() …
我有2个类似的JavaScript函数,但其中一个有一个硬编码变量,而另一个函数的变量在被调用时被定义.对不起,如果我说的话没有意义,但这里是代码,这样你就可以更容易地理解它:
function calculateCircumference()
{
var radius = 3;
var circumference = Math.PI * 2 * radius;
console.log("The circumference is " + circumference);
}
function calculateArea()
{
var radius = 3;
var area = Math.PI * radius * radius;
console.log("The area is " + area);
}
function calculateCircumference(radius)
{
var circumference = Math.PI * 2*radius;
console.log("The circumference is " + circumference);
}
function calculateArea(radius)
{
var area = Math.PI * radius*radius;
console.log("The area is " + area);
}
calculateCircumference();
calculateArea();
calculateCircumference(5); …Run Code Online (Sandbox Code Playgroud)我想创建一个"Tag"类,可以将其名称指定为点分隔名称,"this.is.my.name"或者作为字符串向量,如{"this","is","my","name"}.
当我尝试这样做时,编译器有时会告诉我我的调用是不明确的.我想知道(1)为什么这个含糊不清,以及(2)为什么它有时只是含糊不清.
这是我的示例代码,您也可以在Coliru上查看和编译
#include <string>
#include <vector>
#include <iostream>
class Tag
{
public:
explicit Tag(std::string name);
explicit Tag(std::vector<std::string> name);
};
Tag::Tag(std::string name)
{
//here 'name' will be a dotted collection of strings, like "a.b.c"
}
Tag::Tag(std::vector<std::string> name)
{
//here 'name' will be a vector of strings, like {"a","b","c"}
}
int main(int argc, char**argv)
{
Tag imaTag{{"dotted","string","again"}};
Tag imaTagToo{"dotted.string"};
//everything is fine without this line:
Tag imaTagAlso{{"dotted","string"}};
std::cout << "I made two tags" …Run Code Online (Sandbox Code Playgroud) 当我打印整个集合时,结果是未排序的并且它包含一个副本.该对象Person有姓氏,姓氏和出生年份(所有3个都是字符串).我首先按出生年份排序,然后按姓氏,然后按姓氏排序.本身没有相同的人(但即使是这样,也应该在插入时将其删除set).
更具体一点,我创建了一组这样的人:
std::set <Person> greatUncles;
Run Code Online (Sandbox Code Playgroud)
并插入它们像这样:
greatUncles.insert(Person("bla", "bla", "1900"));
Run Code Online (Sandbox Code Playgroud)
这是课堂上必不可少的东西Person:
class Person {
public:
//...
Person(std::string s, std::string f, std::string y)
:surname(s), familyname(f), yearOfBirth(y)
{
}
//...
std::string getSurname() const {
return surname;
}
std::string getFamilyname() const {
return familyname;
}
std::string getYearOfBirth() const {
return yearOfBirth;
}
private:
std::string surname;
std::string familyname;
std::string yearOfBirth;
};
//to print the set, overload the '<<' operator
std::ostream &operator<<(std::ostream &o, const Person &person) {
o << person.getSurname() …Run Code Online (Sandbox Code Playgroud) 我正在测试一个简单的运算符重载代码,当测试它时,这段代码只是在“ nd.print()”处崩溃(将内核转储)。有什么建议吗?
崩溃发生在Ubuntu 16.04 64位上。当我尝试某些在线shell环境(例如https://www.onlinegdb.com/online_c++_compiler)时,似乎还可以。
#include <iostream>
using namespace std;
class Node
{
int d;
public:
Node (int dd = 0):d(dd){}
Node &operator=(Node &nd){ d = nd.d; }
void print(){ cout<<d<<endl; }
};
int main()
{
Node nd1(1), nd2(2);
Node nd;
nd = nd2 = nd1;
nd.print(); //*******Crash here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望它只是打印一个值而不会崩溃。
为什么bool要调用演员表?
Set result(*this)调用构造函数时出现问题。我希望它使用拷贝构造函数,而不是它转换*this到bool并使用它作为一个int的构造函数。
如何修复它以使用复制构造函数?
Set Set::operator+(const Set& rhs)const
{
Set result(*this);
for (unsigned int i = 0; i < rhs.getSize(); i++)
{
result.add(rhs[i]);
}
return result;
}
Set::operator bool()const
{
return !!(*this);
}
Set::Set(size_t capacity)
{
data = new int[capacity];
size = 0;
this->capacity = capacity;
}
void Set::copy(const Set& copied)
{
size = copied.getSize();
capacity = copied.getCapacity();
if (data != nullptr)
delete[]data;
data = new int[capacity];
for (unsigned int i …Run Code Online (Sandbox Code Playgroud) 给定C#最终基类,为什么System.Collections.Generic.IEnumerable<T>不能分配给参数类型?System.Collections.Generic.IEnumerable<object>object
当执行类似于以下代码的操作时,我偶然发现了这种好奇心。这是一个泛型方法,称为重载非泛型方法。
void Main()
{
List<object> objects = new List<object>();
Method(objects); // This calls the method with IEnumerable, as expected
MethodCaller(objects);
}
void MethodCaller<T> (IEnumerable<T> objects)
{
Method(objects); // Calls method overload 2, with object as parameter - why?
// Why is the following line required to call the method overload 1?
// Can't C# do this automatically, given that object is the ultimate base class for everything?
IEnumerable<object> casted = (IEnumerable<object>) objects;
Method(casted); // Calls …Run Code Online (Sandbox Code Playgroud) 它为以下类型的问题提供了Java解决方案:具有一个在一个参数上有所不同的重载方法。如果将不同的参数作为null传入,该怎么办?
class PersonFactory {
public static Person create(String firstname, String lastname, String age) {
return create(firstname, lastname, Integer.valueOf(age));
}
public static Person create(String firstname, String lastname, Integer age) {
Person p = new Person();
p.setFirstname(firstname);
p.setLastname(lastname);
p.setAge(age);
return p;
}
}
PersonFactory.get("John", "Doe", null); //ambigous mapping
Run Code Online (Sandbox Code Playgroud)
我以为可以通过Optional.empty()这里,但是没有定义的类型,因此不能用来选择正确的方法...
我经常看到这种情况,并且想知道它的用途/目的。
例
std::vector<double> operator()(int seed) const;
Run Code Online (Sandbox Code Playgroud)
谢谢!
overloading ×10
c++ ×6
c# ×2
boolean ×1
c++11 ×1
casting ×1
class ×1
exponent ×1
generics ×1
java ×1
javascript ×1
object ×1
parentheses ×1
set ×1
this ×1