现在我有两个类allmethods.cs和caller.cs.
我在课堂上有一些方法allmethods.cs.我想写一个代码,caller.cs以便在中调用某个方法allmethods.
代码示例:
public class allmethods
public static void Method1()
{
// Method1
}
public static void Method2()
{
// Method2
}
class caller
{
public static void Main(string[] args)
{
// I want to write a code here to call Method2 for example from allmethods Class
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?任何帮助?
谢谢.
首先,问题是:你有什么理由不能从函数中获取函数的地址吗?
#include <stdio.h>
struct foo {
void(*xf)(void);
int r;
} sFoo;
void func(void) {
sFoo.xf = func; /* <-- like this */
}
int main()
{
func();
printf("FuncPtr is: %p\n", sFoo.xf);
printf("FuncAddr is: %p\n", &func);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想不出为什么这不应该移植,但这并不意味着没有一个.我在Windows上使用MinGW和MSVC进行了测试,在Ubuntu上使用gcc进行了测试,它运行正常.C标准几乎是无声的,除了说(C99 5.1.1.2)函数引用在最终转换阶段得到解决.
我的主要恶化是我找不到任何证实这是标准行为的东西.我过去曾经遇到过评论,说你不能从函数中取一个函数的地址,我认为当时这个函数是假的,但实际上没有时间或倾向于实际检查它出来,直到现在.
我正在使用 C++ 创建一个队列模板类。
队列类有多个函数成员。其中一个函数称为 front() 来检索队列的第一个值。基本上,front()函数将首先检查队列是否为空(使用另一个布尔函数is_empty())。
如果为空,函数将抛出错误消息并返回 1 表示有错误。如果队列不为空,则返回第一个值,该值的类型与队列中数据的类型相同。正如您所看到的,有两种不同类型的返回值。在定义函数时如何同时指定这两种类型?
示例代码如下。返回类型是 T。但是该函数也返回 1。这在 C++ 中可以接受吗?如果不是,如何修改?提前致谢!
template <class T>
T MyQueue<T>::front() {
if (! is_empty()) {
return my_queue[0];
}
else {
cout << "queue is empty!" << endl;
return 1;
}
}
Run Code Online (Sandbox Code Playgroud) Visual Studio 显示编译器警告CS04721,其中(int)5 == null读取
表达式的结果始终为“false”,因为“int”类型的值永远不等于“int”类型的“null”?
bool x = (int)5 == null;
Run Code Online (Sandbox Code Playgroud)
DateTime但是,如果使用对象,则不会发出警告。
bool y = DateTime.UtcNow == null;
Run Code Online (Sandbox Code Playgroud)
由于DateTimeis not Nullable<DateTime>,因此它永远不可能为 null 。第二个语句没有显示类似警告的原因是什么?