const 函数的编译器错误

发布于 2024-07-13 21:32:06 字数 495 浏览 3 评论 0原文

我不确定我是否缺少一些基本的东西。 但我无法理解为什么编译器会为此代码生成错误:

class A
{
};

class B
{
public:
    B();
    A* get() const;

private:
    A* m_p;
};

B::B()
{
    m_p = new A;
}

A* B::get() const
{
    //This is compiling fine
    return m_p;
}

class C
{
public:
    A* get() const;
private:
    A m_a;
};

A* C::get() const
{
   //Compiler generates an error for this. Why? 
    return &m_a;
}

编辑:编译器错误是:错误 C2440:'return':无法从 'const class A *' 转换为 'class A *' 转换丢失限定符

I am not sure whether I am missing something basic. But I am unable to understand why the compiler is generating the error for this code:

class A
{
};

class B
{
public:
    B();
    A* get() const;

private:
    A* m_p;
};

B::B()
{
    m_p = new A;
}

A* B::get() const
{
    //This is compiling fine
    return m_p;
}

class C
{
public:
    A* get() const;
private:
    A m_a;
};

A* C::get() const
{
   //Compiler generates an error for this. Why? 
    return &m_a;
}

EDIT: The compiler error is : error C2440: 'return' : cannot convert from 'const class A *' to 'class A *' Conversion loses qualifiers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

挽手叙旧 2024-07-20 21:32:06

函数签名中的const告诉编译器该对象的成员不能被修改。 然而,您返回一个指向成员的非 const 指针,从而违反了该承诺。

在您的类 B 中,您不许诺/不许诺,因为您不返回指向成员的指针,而是返回它的副本(并且该成员恰好是一个指针)。

const in the function signature tells the compiler that the object's members may not be modified. Yet you return a non-const pointer to a member, thus allowing a violation of that promise.

In your class B, you make/break no promise since you don't return a pointer to a member, you return a copy of it (and the member happens to be a pointer).

感受沵的脚步 2024-07-20 21:32:06

这是因为您从 const 函数返回一个指向成员的非常量指针。

第一部分有效,因为您返回成员指针的副本,因此这不会违反 get 函数的常量性:

class B
{
public:
    B();
    A* get() const;

private:
    A* m_p;
};

A* B::get() const
{
    //This is compiling fine
    return m_p;
}

但是下一位会生成编译错误(在 gcc 4 上) )

testfile.cpp:37: 错误:从“const A*”到“A*”的转换无效

因为您的 const get 函数通过返回指向 m_a 的非常量指针来提供对 m_a 的非常量访问。

class C
{
public:
    A* get() const;
private:
    A m_a;
};

A* C::get() const
{
   //Compiler generates an error for this. Why?
    return &m_a;
}

It's because you're returning a non-const pointer to a member from a const function.

The first part works because you're returning a copy of a member pointer, so this doesn't violate the const-ness of the get function:

class B
{
public:
    B();
    A* get() const;

private:
    A* m_p;
};

A* B::get() const
{
    //This is compiling fine
    return m_p;
}

But the next bit generates the compile error (on gcc 4)

testfile.cpp:37: error: invalid conversion from ‘const A*’ to ‘A*’

Because your const get function is providing non-const acess to m_a by returning a non-const pointer to it.

class C
{
public:
    A* get() const;
private:
    A m_a;
};

A* C::get() const
{
   //Compiler generates an error for this. Why?
    return &m_a;
}
巾帼英雄 2024-07-20 21:32:06

因为返回的指针不是const。 将其更改为:

class C
{
public:
    const A* get() const;
private:
    A m_a;
};

const A* C::get() const
{
    //Compiler generates an error for this. Why? 
    return &m_a;
}

请注意,C::get() 现在返回一个指向 A 的 const 指针。

Because the returned pointer is not const. Change it to this:

class C
{
public:
    const A* get() const;
private:
    A m_a;
};

const A* C::get() const
{
    //Compiler generates an error for this. Why? 
    return &m_a;
}

Notice that C::get() now returns a const pointer to A.

骑趴 2024-07-20 21:32:06

标记为 const 的成员函数不能返回非常量引用或指向私有变量的指针。 如果编译器允许这样做,类之外的任何人都可以修改所述私有变量,并且函数上的 const 限定符将失去意义。

Member functions marked const cannot return a non-const reference or pointer to a private variable. If the compiler allowed this, anyone outside your class would be able to modify the said private variable and the const qualifier on the function would lose meaning.

电影里的梦 2024-07-20 21:32:06

这个问题可以用一个更简单的例子来说明:

class MyClass {
public:
    int *get() const;
private:
    int value;
};

int *MyClass::get() const {
    return &value;
}

MyClass::get() const中,value的类型为const int。 当您取消引用它时,您将得到 const int *。 该类型无法安全(隐式)转换为 int *。 要纠正您的问题,请让 get() 返回 const int *

This problem can be illustrated with a simpler example:

class MyClass {
public:
    int *get() const;
private:
    int value;
};

int *MyClass::get() const {
    return &value;
}

In MyClass::get() const, value has the type const int. When you dereference it, you get const int *. That type cannot be safely (implicitly) casted to int *. To correct your problem, have get() return const int *.

独享拥抱 2024-07-20 21:32:06
A* C::get() const
{
   //Compiler generates an error for this. Why? 
    return &m_a;
}

由于 get() 是 const 函数,因此编译器将其引用的所有成员变量视为 const。 当您获取此类成员的地址时,您将获得一个指向 const 的指针。 但你的函数返回一个非常量指针。 您需要将代码更改为

const A* C::get() const
{
    return &m_a;
}
A* C::get() const
{
   //Compiler generates an error for this. Why? 
    return &m_a;
}

Because get() is a const function, the compiler treats all member variables it refers to as const. When you take the address of such a member, you get a pointer to const. But your function is returning a non-const pointer. You need to change your code to

const A* C::get() const
{
    return &m_a;
}
油饼 2024-07-20 21:32:06

基本上只需在前面添加一个 const,

const A* C::get() const
{
   //Compiler generates an error for this. Why? 
    return &m_a;
}

然后如果你想访问它,基本上就这样做:

C something;

const A* a = something.get();

但是,对我来说,你的程序没有什么意义。

IMO,这样做是最有意义的:

class A{
};

class C : public A
{
};

这样你就不必进行返回 A 实例的“get”。

Basically just add a const in front,

const A* C::get() const
{
   //Compiler generates an error for this. Why? 
    return &m_a;
}

Then if you want to access it, basically do:

C something;

const A* a = something.get();

However, your program makes very little sense, to me.

IMO, it would make most sense to do:

class A{
};

class C : public A
{
};

That way you don't have to make a "get" that returns the instance of A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文