函数中的C ++ const参数 [英] C++ const arguments in functions

查看:118
本文介绍了函数中的C ++ const参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将继续使用C ++进行uni的学习,并且遇到一些有关指针,const参数和类实现的所有基本知识的严重理解问题(与Java相比C ++如此简单)

I'm continuing my studies for uni with C++ and I'm running into some serious comprehension issues concerning pointers, const arguments and all the very basics of class implementations (which are so easy in Java compared to C++)

我通常使用Java,所以C ++对我来说是很新的.

I usually work with Java, so C++ is very new to me.

这是我的"Person"类的简单标题:

This is my simple header for a class "Person":

#ifndef Person_h
#define Person_h

class Person
{
private:
    char* name;
    char* adress; 
    char* phone;

public:
    Person();
    Person(char const *name, char const *adress, char const *phone);
    Person(const Person &other);
    ~Person();

    void setName(char const *name);
    char* getName() const;
    void setAdress(char const *adress);
    char* getAdress() const;
    void setPhone(char const *phone);
    char* getPhone() const;
};

#endif // !Person_h

问题就在这里开始.为什么要使用char指针而不是实际的char变量?我猜这是一些保留内存或提高性能的约定吗?

And here the problem starts. Why should I use char pointers instead of actual char variables? I'm guessing it's some conventions to spare memory or to improve performance?

这是我们的教授编写代码的方法,试图使我们理解指针和const等的使用.

This is the way our professor codes and tries to make us understand the use of pointer and const etc.

现在这是我对此类的实现:

Now here's my implementation of the class:

#include "Person.h"
//Person.h class implementation

Person::Person()
{
    Person::name = new (char[64]);
    Person::adress = new (char[64]);
    Person::phone = new (char[64]);
}

Person::Person(const char *name, const char *adress , const char *phone)
{
    Person::name = new (char[64]);
    Person::adress = new (char[64]);
    Person::phone = new (char[64]);

    setName(name);
    setAdress(adress);
    setPhone(phone);
};

Person::Person(Person const &other)
{
    Person::name = new (char[64]);
    Person::adress = new (char[64]);
    Person::phone = new (char[64]);

    setName(other.getName);
    setAdress(other.getAdress);
    setPhone(other.getPhone);
};

Person::~Person()
{
    delete [] name;
    delete [] adress;
    delete [] phone;
};

void Person::setName(const char *name)
{
    this->name = name;
};

char* Person::getName() const
{
    return name;
};

void Person::setAdress(char const *adress)
{
    this->adress = adress;
};

char* Person::getAdress() const
{
    return adress;
};

void Person::setPhone(char const *phone)
{
    this->phone = phone;
};

char* Person::getPhone() const
{
    return phone;
};

我们应该学习手动为元素分配内存,并尝试照顾整个内存管理.因此,在setter函数中使用const参数.我猜这是为了不更改element参数?我很困惑,基本上...

We should learn to manually allocate memory to elements and try to take care of the overall memory management. Therefore the use of const arguments for the setter functions. I guess this is in order to not alter the element argument? I'm very confused, basically...

我的IDE(MS VisualStudio 2015)强调以下行为错误:

And my IDE (MS VisualStudio 2015) underlines the following line as error:

void Person::setName(const char *name)
{
    this->name = name;    //error
};

不能将类型为'const char *'的值分配给类型为'char *'的实体"

"a value of type 'const char *' cannot be assigned to an entity of type 'char *'"

那么当我不能分配这些值时为什么要使用const?或者如何在不使成员变量本身为const的情况下"un-const"这些变量?

Then why should I use const when I can't assign these values? Or how can I "un-const" those, without making the member variable itself const?

这整个事情现在对我来说只是一个大困惑.

This whole matter is just one big confusion to me now.

必须在考试中使用C字符串,目的是为了了解与教授相关的指针和内存管理.

I have to use C-strings for my exams, that's in order to understand pointer and memory management referring to our prof.

推荐答案

用外行的话来说,当name是const而this->name是非const时,this->name = name;不起作用的原因是,您的函数是承诺name的内容将被视为只读,但是通过将此指针分配给非const指针,则可以随意随意修改数据,从而违反了诺言.

In layman's terms, the reason why this->name = name; does not work when name is const and this->name is non-const, is because your function is promising that the contents of name will be treated as read-only, but by assigning this pointer to a non-const pointer you would then be free to modify the data as you please, thus breaking your promise.

您的老师显然正在尝试教您带指针的老式C ++(本质上是面向对象的C),因此您应该不要继续,用字符串替换char指针.如果您这样做,您的老师可能会不开心.

Your teacher is obviously trying to teach you old-style C++ with pointers, (essentially, object-oriented C,) so you should not go ahead and replace char pointers with strings. Your teacher will probably be unhappy if you do that.

我们使用char*而不是char[]的原因有很多.

There is a number of reasons why we use char* instead of char[].

  • 一个原因是效率:如果可以传递char[],则该char[]的全部内容都需要复制到堆栈中.传递char*时,您仅将指针复制到字符,通常是单个机器字.

  • One reason is efficiency: if it was possible to pass a char[], then the entire contents of that char[] would need to be copied to the stack. When you pass a char*, you are only copying the pointer to the characters, which is usually a single machine word.

另一个原因是实际上是不可能的.编译器不了解char[]为零终止的事实,因此无法为其分配足够的堆栈空间并将其复制到堆栈中.该语言的创建者希望不要在语言中加入这种内置的知识.相反,他们决定将char[]隐式转换为char*,以便您可以在函数中将其作为char*接收,并随心所欲地进行操作.

Another reason is that it is actually impossible. The compiler has no built-in knowledge of the fact that your char[] is zero-terminated so as to allocate enough stack space for it and copy it to the stack. The creators of the language preferred to not put such built-in knowledge in the language. Instead, they decided that a char[] should be implicitly converted to char*, so that from within your function you can receive it as a char* and do what you want with it.

另一个原因是,如果您的char[]太大了,那么您的堆栈就会溢出.

Another reason is that if your char[] was just a bit too large, then your stack would overflow.

因此,由于所有这些原因,当我们要传递比机器字大(或大得多)的类型的值时,我们不传递值本身,而是传递指向它们的指针或引用.

So, for all these reasons, when we want to pass values of types that are larger (or considerably larger) than machine words, we do not pass the values themselves, we pass pointers or references to them.

这篇关于函数中的C ++ const参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆