C++ 对象引用

标签 c++ object

我有这段代码:

Wmmbid02 wmmbid02;
wmmbid02.Omplir(this);
return wmmbid02;

和 Wmmbid02.h:

class Wmmbid02: public Idoc {

public:
    Wmmbid02();
    //void EscriureFitxer();
    void Omplir(Edi);

private:
    Segment crearSegment(string a[], string b[]);
    Info crearInfo(string);
    //virtual void LlegirFormat(string);
};

Visual Studio,返回错误,因为无法将“Edi *const”转换为“Edi”。

正确答案是什么???

谢谢

最佳答案

您的 Omplir 声明

void Omplir(Edi);

表示此方法接受一个 Edi 对象。

在 C++ 中,this 是一个指针。您应该在类中使用它。

如果你想让它工作,你应该取消引用它:

wmmbid02.Omplir(*this);

关于this指针的标准是这样说的:

9.3.2 The this pointer [class.this]

In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, [...]

所以在你的情况下,你似乎在类 Edi 的成员函数中,this 应该是 Edi* 类型.

关于C++ 对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18868004/

相关文章:

c++ - 为什么 int 不能用作返回类型的左值,而用户定义的类可以?

c++ - 大小如何工作?我怎么写自己的?

C++ 类 - 如何从函数返回自定义类型的 vector ?

java - 永久java对象

c# - 如果引用的对象被删除,引用变量会发生什么?

c++ - 64 位驱动程序

c++ - 模板类的内部类可以是非模板类吗?

object - 是否可以在golang中同时指定带变量的结构和接口(interface)?

java - 如果请求在同一天到来,则对象保持不变,否则比较并创建新对象

c# - 如何从 C# 中存储为对象的 object[] 进行转换