const成员变量在C ++ 11 [英] Const Member Variables in C++11

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

问题描述

代码

  category :: category(const std :: string p_name,std :: string p_ImagePath):
m_name {p_name},
m_ImagePath {p_ImagePath}
{

}

header

  #pragma once 
#include< string&
class category
{
public:
const int i;
显式类别(const std :: string p_name,const std :: string p_ImagePath);
〜category();
std :: string GetName();
private:
std :: string m_name;
std :: string m_ImagePath;
};

由于作业操作员,我总是会收到错误



Fehler 1 error C2280:'booking& booking :: operator =(const booking&)':试图引用一个已删除的函数C:\Program Files(x86)\Microsoft Visual C ++ Compiler 2013年11月CTP \ include /\\utility 53



如果我尝试在类中使用const成员变量或const静态成员变量。

 我试过const i = 5; 
static const i = 5;
和const i; - >我在构造函数中初始化。

没有什么工作,我如何解决这个问题?
我不能使用constexpr由于vs2013不帮助它:(



我已经在Stackoverflow检查了一些问题,但一切都是用constexpr

解决方案

您必须明确定义复制赋值运算符 > class 有一个常量非静态



此外,还不清楚常数的含义是什么 c>定义中的 $ ,非静态数据成员。
$ b

对于构造函数,您可以删除函数说明符 explicit 并定义参数作为常量引用。

  category(const std :: string& p_name,const std :: string& p_ImagePath); 


code

   category::category ( const std::string p_name , std::string p_ImagePath) :
    m_name { p_name },
    m_ImagePath {p_ImagePath }
    {

    }

header

#pragma once
#include <string>
class category
{
public:
    const int i;
    explicit category ( const std::string p_name ,const std::string p_ImagePath);
    ~category ( );
    std::string GetName ( );
private:
    std::string m_name;
    std::string m_ImagePath;
};

I allways get errors due to assignment opperator

Fehler 1 error C2280: 'booking &booking::operator =(const booking &)' : attempting to reference a deleted function C:\Program Files (x86)\Microsoft Visual C++ Compiler Nov 2013 CTP\include\utility 53

if i try to use a const member variable or a const static member variable in a class.

I tried const i = 5;
static const i = 5;
and const i; -> i gets initialized in constructor.

Nothing works, how can i fix this? And i cant use constexpr due to vs2013 does not assist it :(

I already checked some questions on Stackoverflow but everything was with constexpr

解决方案

You have to define the copy assignment operator explicitly. As your class has a constant, non-static data member then the compiler defined copy assignment operator is deleted.

Also it is totally unclear what is the meaning of that constant, non-static data member in your class definition.

As for the constructor, you may remove function specifier explicit and define the parameters as constant references.

category( const std::string &p_name, const std::string &p_ImagePath );

这篇关于const成员变量在C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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