小编Maj*_*jak的帖子

C++返回值,引用,const引用

你能解释一下返回值,引用值和const引用值之间的区别吗?

值:

Vector2D operator += (const Vector2D& vector)
{
    this->x += vector.x;
    this->y += vector.y;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

非const引用:

Vector2D& operator += (const Vector2D& vector)
{
    this->x += vector.x;
    this->y += vector.y;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

Const参考:

const Vector2D& operator += (const Vector2D& vector)
{
    this->x += vector.x;
    this->y += vector.y;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

这有什么好处?我理解const引用传递给函数背后的意义,因为你要确保不要修改引用指向函数内部的这个值.但我对返回const引用的含义感到困惑.为什么返回引用比返回值更好,为什么返回const引用要比返回非const引用更好?

c++ reference const-reference return-by-reference return-by-value

40
推荐指数
4
解决办法
6万
查看次数

C#Win Api DDE连接多线程

我在C#中使用Win Api实现了DDE客户端的功能.如果我打电话DdeInitializeWDdeConnect单线程,一切正常.具体来说,这些是包装器定义:

    [DllImport("user32.dll")]
    protected static extern int DdeInitializeW(ref int id, DDECallback cb, int afcmd, int ulres);

    [DllImport("user32.dll")]
    static extern IntPtr DdeConnect(
        int idInst,             // instance identifier
        IntPtr hszService,      // handle to service name string
        IntPtr hszTopic,        // handle to topic name string
        IntPtr pCC              // context data
        );
Run Code Online (Sandbox Code Playgroud)

如果我打电话DdeInitializeW,并DdeConnect在不同的线程,DdeConnect返回空指针.

此外,如果我在一个线程中调用它们(已建立的DDE连接),我不能在另一个线程中使用此DDE通道(我收到INVALIDPARAMETERDDE错误).

正如我所说,一切都在单线程中没有问题.

c# multithreading dde

7
推荐指数
1
解决办法
872
查看次数

C++将基类和派生类对象存储在一起

我有两节课:

第一:

class Thing {
public:
    int code;
    string name;
    string description;
    int location;
    bool canCarry;

    Thing(int _code, string _name, string _desc, int _loc, bool _canCarry) {
        code = _code;
        name = _name;
        description = _desc;
        location = _loc;
        canCarry = _canCarry;
    }
};
Run Code Online (Sandbox Code Playgroud)

第二:

class Door: public Thing {

private:
    bool open;

public:

    int targetLocation;

    Door(int _code, string _name, string _desc, int _loc, int _targetLoc) :
            Thing(_code, _name, _desc, _loc, false) {
        open = false;
        targetLocation = _targetLoc;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ storage derived base object

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

使用BitmapData和C#中的指针进行快速位图修改

我正从一些相机(RAW数据阵列)捕获数据.

然后我根据调色板将这些数据映射到RGB值.

我需要尽可能快地映射它,所以我使用BitmapDdata指针在不安全的代码片段中使用和编辑像素.

public void dataAcquired(int[] data)
{
    Bitmap bmp = new Bitmap(width, height); 
    BitmapData data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

    for (int i = 0; i < data.Length; i++)
    {
        int x = i % bmp.Width;
        int y = i / bmp.Width;
        Rgb rgb = mapColors[data[i]];

        unsafe
        {
            byte* ptr = (byte*)data.Scan0;
            ptr[(x * 3) + y * data.Stride] = rgb.b;
            ptr[(x * 3) + y * data.Stride + 1] = rgb.g;
            ptr[(x * …
Run Code Online (Sandbox Code Playgroud)

c# performance lockbits bitmapdata

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