为什么我不能使用“const string* sp = 0”在构造函数中初始化

发布于 2024-12-17 10:52:05 字数 742 浏览 1 评论 0原文

我的问题是 const string* p 给了我一个错误。这有什么问题吗?因为我不会改变原来的值。 const int& n = 0 工作正常。

#include <iostream>
using namespace std;
class HasPtr
{
private:
    int num;
    string* sp;
public:
    //constructor
    HasPtr(const int& n = 0, const string* p = 0): num(n), sp(p) {}
    //copy-constructor
    HasPtr(const HasPtr& m): num(m.num), sp(m.sp) {}
    //assignment operator
    HasPtr& operator=(const HasPtr& m)
    {
        num = m.num;
        sp = m.sp;
        return *this;   
    }
    //destructor
    ~HasPtr() {}
};
int main ()
{
    return 0;
}

输出错误是:

error: invalid conversion from ‘const std::string*’ to ‘std::string*’

my problem is that const string* p gives me an error. What is wrong with this? since I am not change the original value. const int& n = 0 works fine.

#include <iostream>
using namespace std;
class HasPtr
{
private:
    int num;
    string* sp;
public:
    //constructor
    HasPtr(const int& n = 0, const string* p = 0): num(n), sp(p) {}
    //copy-constructor
    HasPtr(const HasPtr& m): num(m.num), sp(m.sp) {}
    //assignment operator
    HasPtr& operator=(const HasPtr& m)
    {
        num = m.num;
        sp = m.sp;
        return *this;   
    }
    //destructor
    ~HasPtr() {}
};
int main ()
{
    return 0;
}

output Error is :

error: invalid conversion from ‘const std::string*’ to ‘std::string*’

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

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

发布评论

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

评论(3

伊面 2024-12-24 10:52:05
private:
    int num;
    string* sp;

sp 是非常量,但 p 是:

const string* p = 0

结果是这个 sp(p) 基本上是这样的:

string* sp = (const string*)0;

它在抱怨,因为这样做会删除字符串的 const 性质。

private:
    int num;
    string* sp;

sp is non-const, but p is:

const string* p = 0

The result is that this sp(p) is basically this:

string* sp = (const string*)0;

It's complaining because doing this would remove the string's const-ness.

橪书 2024-12-24 10:52:05

这是因为您的 sp 成员不是 const

string* sp;

但是你的参数p是一个const。结果是您试图将 const 指针分配给非常量指针 - 因此会出现错误。

要解决此问题,您还需要声明 sp const at 。

const string* sp;

This is because your sp member is not const.

string* sp;

But your parameter p is a const. The result is that you are trying to assign a const pointer to a non-const pointer - hence the error.

To fix this, you need to declare sp const at as well.

const string* sp;
千と千尋 2024-12-24 10:52:05

我认为您对 const 的各种含义感到困惑。

 const string*sp;

声明一个指向常量对象的指针,该指针只允许访问类 string 的常量方法。

 string*const sp;

将指向字符串的指针声明为类的常量成员,您必须在构造函数中初始化 sp 并且不能更改(使用 const_cast<> 除外)。

 const int#

在参数列表中意味着该函数承诺不会更改 num 引用的整数的值,但它当然可以复制其值(就像您所做的那样)。字符串指针的相应操作本来就是

 HasPtr(string*const&p) : sp(p) { /* ... */ }

完全合法的,尽管相当非正统。

I think you got confused by the various meanings of const.

 const string*sp;

declares a pointer to a constant object, which only allows access to constant methods of class string.

 string*const sp;

declares a pointer to a string to be a constant member of the class, which you must initialise sp in the constructor and cannot change (except using const_cast<>).

 const int#

in the argument list means that the function promises not to alter the value of the integer referred to by num, but it can of course copy its value (as you did). The corresponding operation for the string pointer would have been

 HasPtr(string*const&p) : sp(p) { /* ... */ }

and would have been perfectly legal albeit rather unorthodox.

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