标签: const

使用修改后的指向const的指针返回指向非const的指针

const从函数返回指针到非的最佳实践是什么,其中指针是通过修改(非const)指针到const?获得的.像这样:

NODE *top_level(const NODE *input)
{
  while (input->parent != nullptr)
    input = input->parent;  // NODE::parent is (non-const) NODE*

  return input;  // Compile failure: 
                 // Cannot convert from 'const NODE *' to 'NODE *'
}
Run Code Online (Sandbox Code Playgroud)

我可以const_castconst上回走,这似乎很好,但有没有更好的办法?

c++ const

0
推荐指数
1
解决办法
127
查看次数

创建指向"this"对象的指针

我似乎在一个项目中遇到问题,试图创建一个指向"this"的指针,其中"this"是C++列表中的第一个LinkedList.第一个对象中有数据,第二呢...等,直到this->m_nextISNULL

编译器正在向我吐口水:

linkedlist.hpp:55:22: error: invalid conversion from âconst LinkedList<int>* constâ to âLinkedList<int>*â [-fpermissive]

我究竟做错了什么?

template <typename T>  
int LinkedList<T>::size() const
{
  int count = 0;
  LinkedList* list = this; // this line is what the compiler is complaining about

  //adds to the counter if there is another object in list
  while(list->m_next != NULL)
  {
    count++;
    list = list->m_next;
  }
  return count;
}
Run Code Online (Sandbox Code Playgroud)

c++ const

0
推荐指数
1
解决办法
112
查看次数

为什么这段c ++没有找到struct member'filename'?

            for (torrent_info::file_iterator i = t.begin_files();
                    i != t.end_files(); ++i, ++index)
            {
                    int first = t.map_file(index, 0, 1).piece;
                    int last = t.map_file(index, i->size - 1, 1).piece;
                    std::cout << "  " << std::setw(11) << i->size
                            << " " << i.filename() << "[ " << first << ", "
                            << last << " ]\n";
            }
Run Code Online (Sandbox Code Playgroud)

编译给我以下错误:

error: ‘class __gnu_cxx::__normal_iterator<const libtorrent::internal_file_entry*, std::vector<libtorrent::internal_file_entry, std::allocator<libtorrent::internal_file_entry> > >’ has no member named ‘filename’
Run Code Online (Sandbox Code Playgroud)

AFAICS i是恒定的internal_file_entry结构,其标题代码住在这里的开源项目libtorrent.我只是第一次看C++但我不能为我的生活解决为什么上面的调用i.filename()在编译时失败了?

c++ struct const vector

0
推荐指数
1
解决办法
124
查看次数

需要C++静态方法

我有一个方法run()成员MyClass.编译时,我明白了

    Error   3   error C2662: 'MyClass::run' : 
    cannot convert 'this' pointer from 'const MyClass' to 'MyClass&'
Run Code Online (Sandbox Code Playgroud)

ITOH,如果我把这个方法设为静态,我没有错误.方法调用发生在这里:

Errors MyClass::execute( const AbstractExecutionContext &ctx ) const
{
    Errors errs;

    Watch wat; wat.restart();
    {

        run() ;

    }

    return errs;
}
Run Code Online (Sandbox Code Playgroud)

和这种方法的声明是

Errors execute(const AbstractExecutionContext &ctx) const;
Run Code Online (Sandbox Code Playgroud)

我希望我可以使这个方法不是静态的,因为如果它是静态的,run()调用的方法也必须是静态的,并且无法访问非静态的数据成员(我必须丑陋地将它们作为参数传递给方法) .

编译错误的原因是什么,什么是解决方案?

c++ static static-methods const

0
推荐指数
1
解决办法
174
查看次数

为什么在这种情况下不隐式生成operator =和copy-constructor?

我有这样的结构:

/* Renderable definition */
    struct Renderable
    {
        Renderable(VertexBufferPtr vertexBuffer, const Mat4& wvpMatrix, const Mat4& worldMatrix, const Vec4& diffuseColor, const float specularFactor) : 
                    mVertexBuffer(vertexBuffer), mTransform(wvpMatrix, worldMatrix), mMaterial(diffuseColor, specularFactor)
        {
        }

        /* Transform definition */
        struct Transform
        {
            Transform(const Mat4& wvpMatrix, const Mat4& worldMatrix) : mWVPMatrix(wvpMatrix), mWorldMatrix(worldMatrix)
            {
            }

            const Mat4 mWVPMatrix;
            const Mat4 mWorldMatrix; 
        };

        /* Material definition */
        struct Material
        {
            Material(const Vec4& diffuseColor, const float specularFactor) : mDiffuseColor(diffuseColor), mSpecularFactor(specularFactor)
            {
            }

            const Vec4 mDiffuseColor;
            const float mSpecularFactor;
        }; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor compiler-errors const copy-constructor

0
推荐指数
1
解决办法
454
查看次数

为什么关于const引用的代码示例在c ++中是不同的

const string &str1="test";

string &str2="test";
Run Code Online (Sandbox Code Playgroud)

为什么str1可以编译但str2不能?我无法理解,谢谢,

c++ string const

0
推荐指数
1
解决办法
70
查看次数

C++在构造函数中定义const

我在第30行遇到错误(const Date date2 = new Date(2012年12月31日);)

错误消息是:从'Date*'转换为请求的非标量类型'const Date'

以下是我的源代码:

class Date
{
private :
    int day ;
    int month ;
    int year ;
public :
    Date(){
        day = 1;
        month = 1;
        year = 2000;
    }
    Date(int d, int m, int y) : day(d), month(m), year(y){
    }
    int getDay () const { return day ;}
    int getMonth () const { return month ;}
    int getYear () const { return year ;}
};

int main ()
{
    const Date date ; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor const

0
推荐指数
1
解决办法
158
查看次数

无法使用标头枚举

我想做以下事情:

parser.h

#ifndef WS_PARSER_H
#define WS_PARSER_H
#include <stdin.h>
#include <stdbool.h>

typedef enum {
    FIN = 0x80;
    RSV1 = 0x40
    /* ... */
} ws_flags;

#endif
Run Code Online (Sandbox Code Playgroud)

parser.c

#ifndef WS_PARSER
#define WS_PARSER
#include "parser.h"

ws_read_fin_flag(unsigned char * chunk) {
    return (bool) chunk[0] & WS_FLAGS.FIN;
}

#endif
Run Code Online (Sandbox Code Playgroud)

不幸的是,我得到的FIN是未声明的标识符.

我做错了什么?

更新:

全球枚举的惯例是什么?

typedef enum {} WS_FLAGS;
Run Code Online (Sandbox Code Playgroud)

c enums const header

0
推荐指数
1
解决办法
462
查看次数

C++方法只有返回类型(和const)的'constness'不同

我偶然发现了一些我以前从未见过的东西.考虑一下你有以下课程:

class foo
{
    const bar* get() const;
    bar* get();
}
Run Code Online (Sandbox Code Playgroud)

foo的客户端如何决定使用哪种get()方法?

c++ overloading const

0
推荐指数
1
解决办法
154
查看次数

数组运算符[]重载const和非const版本

我得到了一个实现模板数组类的任务.其中一个要求是重载[]运算符.我制作了这两个const和非const版本,似乎工作正常.

const T& operator[](const unsigned int index)const
Run Code Online (Sandbox Code Playgroud)

T& operator[](const unsigned int index)
Run Code Online (Sandbox Code Playgroud)

我的问题是当我做类似的事情时,编译器将如何知道运行哪一个:

int i=arr[1]
Run Code Online (Sandbox Code Playgroud)

在非const数组?

c++ const operator-overloading

0
推荐指数
1
解决办法
2665
查看次数