我在C/C++中经常使用这个宏:
#define MYLOG(x) (std::cout << "Value of " << #x << " is: " << x << std::endl);
它的用法如下:
int x = 1;
int y = 2;
MYLOG(x+y);
结果是:
Value of x+y is: 3
我还没有在任何不使用C预处理器的语言中复制这段代码。在任何其他语言中都可以吗?我希望能够在其他地方开始使用它。注意: eval不算数。我希望在我的代码中看到表达式,而不是字符串,这样
package com.sample;
import java.lang.reflect.Field;
class Demo {
int i;
}
public class DifferenceGenericStringAndToString {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Class c = Demo.class;
Field[] fields = c.getDeclaredFields();
我正在用Ocaml写一个小编译器。在ast.mli中,我定义了两种表达式
type int_expr =
| Integer_constant of int
| Evar of string
| Ebinop of binop * int_expr * int_expr
| Ecell of int_expr * int_expr (* Sheet[ , ] *)
type bool_expr =
| Bool_constant of bool
| Bcmp of cmp * int_expr * int_expr
| Band of bool_expr * b
我有一个关于隐式类型转换的问题
为什么这种隐式类型转换在C#中有效?我了解到隐式代码通常是不起作用的。
我在这里有一个关于隐式类型转换的代码示例
char c = 'a';
int x = c;
int n = 5;
int answer = n * c;
Console.WriteLine(answer);
我对.Net相当陌生,我正在研究一个基本对象数组将如何处理不同类型。我倾向于在开关中使用GetType().ToString()组合来集中处理代码中的事件,并遇到以下返回值。我已经在这里和其他地方寻找了答案(包括C#规范),但是找不到直接解决这个问题的地方:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Objectify
{
class Program
{
static void Main(string[]
考虑下面的代码,它是计算几何中“圆刺问题”的解决方案,即找到与最大数量的非相交圆相交的直线。
这里的主要问题是为什么Python代码比C++代码慢42倍。是不是因为Python使用不当?或者是因为Python在算术和数学运算上天生就比C++慢?有什么办法可以让它更快一点吗?
首先,Python代码:
from __future__ import division
from math import sqrt, atan2, pi
from sys import stdin
__author__ = "Sahand Saba"
EPS = 1e-6
class Point
我正在做一项填充空白数据类型的任务。我已经重读了无数次“学习游戏开发的C++”中的小章节,并尝试了各种类型,但我的问题是,我的问题是从枚举中调用绿色。 以下是原始代码: // DataTypes.cpp : The data types to declare each of the variables is missing.
// Based on the value being stored in the variable and the comments beside it,
// fill in the data type at the beginning of each line.
我注意到C#编译器不会推断第二个泛型参数。
示例:
C++模板代码:(是的,我知道模板不像泛型那样工作)
class Test {
public:
template <class T,class V>
T test(V v) {
//do something with v
return T();
}
};
int i = 0;
Test t = new Test();
double j = t.test<double>(i); //infers V as int
模板(和泛型)不能推断返回类型,所以在C++中,我给它提供了
class A {
public:
void show() {
cout << "Class A Show"<<'\n';
}
};
class C : public A {
public:
void show() {
cout << "Class C Show" << '\n';
}
};
int main() //first int main()
{
C* a = new C; //line 1
正如在C3.5中发现的,由于类型擦除,以下情况是不可能的:
int foo<T>(T bar)
{
return bar.Length; // will not compile unless I do something like where T : string
}
foo("baz");
我相信这在C#和java中不起作用的原因是由于一种称为类型擦除的概念,请参阅。
在阅读了关键字之后,我写了以下内容:
int foo<T>(T bar)
{
dynamic test = bar;
return test.Length;
}
调用C-函数时,可以通过传递可以转换/提升为更宽类型的窄类型来调用更宽的类型。
例如:
void FooBar(uint32_t alpha); // Function takes a 32-bit unsigned value
int main(void)
{
uint16_t foo = 232; // Variable foo is 16-bit unsigned
FooBar(foo); // 16-bit value gets auto-promoted to 32-bit value for the function call.
}
然而,当我
我有一段代码可以在C中工作,但不能在C++中工作,有没有办法让它在C和C++上都能工作呢?
void foo(void* b)
{
int *c = b;
printf("%d\n",*c);
}
int main ()
{
int a = 1000;
foo(&a);
return 0;
}
输出:
C++:
1 In function 'void foo(void*)':
2 Line 3: error: invalid conversion from 'void*' to 'int*'
3
我有一个C#方法,它使用"params“修饰符来表示二维数组。
/// <summary>
/// Combine vectors horizontally.
/// </summary>
///
public static T[] Concatenate<T>(params T[][] vectors)
{
int size = 0;
for (int i = 0; i < vectors.Length; i++)
size += vectors[i].Length;
T[] r = new T
今天,我试验了编译器如何确定声明为var的数字的类型。
var a = 255; //Type = int. Value = byte.MaxValue. Why isn't this byte?
var b = 32767; //Type = int. Value = short.MaxValue. Why isn't this short?
var c = 2147483647; //Type = int. Value = int.MaxValue. int as expected.
var d = 2147483648; //Type = uint. Value = in
我是java的新手。但我经常使用c++。我有一个用c++写的类。现在我需要用java重写这个类。我把泛型类型搞糊涂了。
我的c++类是这样的:
//Form, Cmpt are of class type. nCmpt is a integer
template <class Form, class Cmpt, int nCmpt>
class VectorSpace
{
public:
//component array
Cmpt v[nCmpt];
//constructors
VectorSpace();
VectorSpace(const V
我想知道为什么这段代码会编译在go中。我预期会出现编译器错误,就像使用字符串一样:
package main
import (
"fmt"
)
type myMap map[string]int
type mySlice []int
type myChan chan int
type myString string
func main() {
var mm myMap
var m map[string]int
m = mm // fine, but expected compiler error
var ms mySlice
在C#中,假设我有两个接口:Foo<in P, out C>和Bar<C>。另外,假设我有两个Bar<int>实现:BarOne和BarTwo。
我想在Bar<C>上添加一个方法Baz(),它返回如下内容:
public interface Bar<C> {
// other methods, some of which use C as a type
// then:
Foo<..., C> Baz ();
}
public class BarOne : Bar<int> {
// othe
因此,正如您在C和C++中所知道的,如果使用Visual-C,您可以拥有内联汇编指令,例如:
int main() {
printf("Hello\n");
__asm int 3
printf("this will not be printed.\n");
return 0;
}
这将在可执行文件中创建一个断点。所以我的问题是,有没有什么函数可以用来调用__asm,通过一个变量,比如字符数组。我是这样想的:
char instruction[100] = "int 3";
__asm instruction
但这
我知道在C++中,泛型实际上并不存在,但是您可以使用template来模拟它。当您构建代码时,编译器会对代码进行预处理,并生成一个新代码,替换为对象声明中指定的实际值的泛型值,然后是真正编译的新代码。例如,假设类A如下所示:
template<class T>
class A
{
T f();
};
然后在代码中的其他地方,我们有A<int> a;。实际编译的代码是:
class A
{
//Replaces T by int in the pre-processing
int f();
};
在这整个介绍之后,让我们讲到重点。
我的问题是:
一位采访者说:“天才并不完全是天才”,
他提供了这个例子(参数int k,int d不是泛型的)。
public static void PrintThis<T>(T a, T b, T c, int k,int d)
{
}
他问我,如果我证明它仍然是仿制药,我将被允许参加下一轮。我不知道他对我的期望是什么,也不知道他以身作则是什么意思。
指导我如何聪明地面对这样一个奇怪的面试?
提前谢谢。
假设我有someClass类
class someClass<Generics> {}
someClass<int> a = someClass<int>();
someClass<String> b = someClass<String>();
someClass c = someClass();
如何检查元素a,b,c是否为相同类型(someClass)?
在达特有可能吗?
我尝试使用runtimeType方法,但类型不同。