小编Mar*_*ram的帖子

将子类的枚举器强制转换为父类的枚举器是错误的吗?

我的构建中出现错误,其中说:

错误12无法将类型'System.Collections.Generic.IEnumerator <BaseClass>'隐式转换为'System.Collections.Generic.IEnumerator <IParentClass>'.存在显式转换(您是否错过了演员?)

简单地把它扔掉是不对的?

这是我的代码:

public Dictionary<Int32, BaseClass> Map { get; private set; }

public IEnumerator<BaseClass> GetEnumerator()
        {
            return this.Map.Values.GetEnumerator();
        }

public IEnumerator<IParentClass> IEnumerable<IParentClass>.GetEnumerator()
        {
            return this.GetEnumerator(); // ERROR!
        }
Run Code Online (Sandbox Code Playgroud)

我的问题是,我可以改变这一行:

return this.GetEnumerator();
Run Code Online (Sandbox Code Playgroud)

至:

return (IEnumerator<IParentClass>)this.GetEnumerator();
Run Code Online (Sandbox Code Playgroud)

(没有任何不良副作用)?

一般承认的答案:
我已将功能更改为以下内容(阅读Jon Skeet的帖子后):

IEnumerator<IParentClass> IEnumerable<IParentClass>.GetEnumerator()
        {
            return this.Map.Values.Cast<IParentClass>().GetEnumerator();
        }
Run Code Online (Sandbox Code Playgroud)

.net c# generics covariance

3
推荐指数
1
解决办法
1952
查看次数

乘以矩阵

这两段伪代码有什么区别?

// Multiplying a matrix by the difference between each frame
float difference = current - previous; // Time since previous frame
float angle = difference / 500;
matrix rotation;
rotation.RotateX(angle);
rotation.RotateY(angle);
worldMatrix *= rotation; // Note multiply

// Multiplying a matrix by the difference between current and start
float difference = current - start; // Time since first frame
float angle = difference / 500;
matrix rotation;
rotation.RotateX(angle);
rotation.RotateY(angle);
worldMatrix = rotation; // Note assignment
Run Code Online (Sandbox Code Playgroud)

每段代码之间只有很小的差异,但会导致很大的视觉差异.输入看起来像这样:

第1帧:旋转= 1弧度
worldMatrix*=旋转; …

opengl math 3d direct3d matrix

3
推荐指数
1
解决办法
800
查看次数

将模板参数作为目标类型传递

在这个缩短的示例(不是真实世界的代码)中,我试图Callback用一个调用int &,但是,当通过该CallMethod方法时,模板参数被解释为一个int,这意味着它无法将其转换为目标参数类型.

这可能吗?我知道我可以在调用时将参数强制转换为正确的类型CallMethod,但是如果可能的话,我希望解决方案是隐式的.

#include <functional>
#include <iostream>

using namespace std;

void Callback(int &value)
{
    value = 42;
}

template <typename Method, typename ...Params>
void CallMethod(Method method, Params ...params)
{
    method(std::forward<Params>(params)...);
}

int main()
{
    int value = 0;
    CallMethod(&Callback, value);

    cout << "Value: " << value << endl;

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

c++ templates c++11

3
推荐指数
1
解决办法
82
查看次数

使用可见性属性(不使用 __declspec(dllexport))时,Clang 可以生成导入库吗?

使用 Microsoft 编译器时,要从库中导出类或函数,您将使用类似于以下的代码:

class __declspec(dllexport) Foo {};

使用 Clang(和 GCC),您可以使用 Visibility 属性来确保符号可见:

class __attribute__((visibility(default))) Foo {};

或者依赖编译时设置的可见性。

当我在 Windows 上使用 Clang 进行编译时,如果类可见(即使具有显式属性),则不会导出该函数(不会创建导入库)。

是否可以让 Clang 生成导入库而不使用__declspec(dllexport)Clang大多数但不完全支持的 Microsoft 扩展。

c++ windows dll clang

3
推荐指数
1
解决办法
5633
查看次数

Win32/MFC从客户端矩形获取窗口rect

我知道有一个函数可以接受客户端rect,它会将它转换为一个窗口rect.我只是找不到/记住它!

有谁知道它是什么?

它会做类似的事情:

const CRect client(0, 0, 200, 200);
const CRect window = ClientRectToWindowRect(client);
SetWindowPos(...)
Run Code Online (Sandbox Code Playgroud)

c++ windows winapi mfc

2
推荐指数
1
解决办法
1万
查看次数

Flex 3方法可以检测调用对象吗?

如果我有一个方法如:

private function testMethod(param:string):void
{
  // Get the object that called this function
}
Run Code Online (Sandbox Code Playgroud)

在testMethod中,我可以找出一个叫做我们的对象吗?例如

class A
{
  doSomething()
  {
    var b:B = new B();
    b.fooBar();
  }
}

class B
{
  fooBar()
  {
    // Can I tell that the calling object is type of class A?
  }
}
Run Code Online (Sandbox Code Playgroud)

apache-flex flex3 actionscript-3

2
推荐指数
1
解决办法
4820
查看次数

是否可以使用GoTo?

我目前正在.Net Framework 2.0中用C#重写旧的VB6程序(不是我的选择,它是由公司决定的).在大多数情况下,事情进展顺利.该程序测量来自精密磨床的输入数据,并显示图形和刻度盘以显示精度.

最初的程序员是机械工程师,但不是软件工程师.该程序有效,但这里和那里有一些草率的代码.最值得注意的是,我遇到了一些GoTo语句.在必要时将事物放在一个循环中并且从中获得相同的功能非常容易.

我遇到了原始代码中的一个案例,然而,似乎GoTo不仅仅是模拟一个循环.它有几个不同的退出条件.它看起来像这样(不是实际的代码,只是我做的简短演示):

VB6代码

Public Sub Tick()
    Dim condition1 As Boolean
    Dim condition2 As Boolean
    Dim testNumber As Integer

    beginning:    'The GoTo label'

    ' (... Some Other Code Here ...)'

    If condition1 = True Then
        goto beginning
    Else
        ' (... Do some calculation ...)'
    End If

    If condition2 = True Then
        ' (... Do some calculation ...)'
        goto beginning
    End If

    Select Case testNumber
        Case 1: '(... Some code ...)'
        Case 2: '(... Some code ...)'
        Case 3: …
Run Code Online (Sandbox Code Playgroud)

c# vb6 label goto

2
推荐指数
1
解决办法
533
查看次数

如何创建照片分享活动?

如何在查看照片库时创建可由用户通过共享选项选择的自定义活动?我有像"与Facebook,Twitter,FlickR分享"等选项.但我想在那里添加我自己的选项.

即转到"照片",然后单击"共享"按钮.您将看到一堆共享提供商.我需要做什么才能让我的活动在那里?

android

2
推荐指数
1
解决办法
2983
查看次数

如何使用HttpClient(或其他HTTP类)异步检索JSON

我从HttpClient获得了内容,但我不知道下一步要去哪里,以便解析我的JSON结果.另外,如何异步执行此操作以便我可以向用户显示等待对话框,并选择取消当前请求(不关心UI示例,如何设置HTTP类可取消)?

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://mysite/test.json");
    try
    {
        HttpResponse response = client.execute(request);
                    // What if I want to cancel now??
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
    }
Run Code Online (Sandbox Code Playgroud)

android json asynchronous http

2
推荐指数
1
解决办法
4820
查看次数

结构成员初始化

在阅读DirectWrite源代码时,我遇到了以下结构:

/// <summary>
/// Line breakpoint characteristics of a character.
/// </summary>
struct DWRITE_LINE_BREAKPOINT
{
    /// <summary>
    /// Breaking condition before the character.
    /// </summary>
    UINT8 breakConditionBefore  : 2;

    /// <summary>
    /// Breaking condition after the character.
    /// </summary>
    UINT8 breakConditionAfter   : 2;

    /// <summary>
    /// The character is some form of whitespace, which may be meaningful
    /// for justification.
    /// </summary>
    UINT8 isWhitespace          : 1;

    /// <summary>
    /// The character is a soft hyphen, often used to indicate hyphenation …
Run Code Online (Sandbox Code Playgroud)

c++ struct

2
推荐指数
1
解决办法
276
查看次数