-1

Strange behaviour. I was developing android native app with c++, and got bug. Some function was not called for some reason. after some revert and compare..

This made program trouble..

const std::string STR_PATH_ASSET("assets/");
const std::string STR_PATH_SD("/sdcard/unlock_data/assets/");
const std::string STR_SUFFIX_PNG(".png");
const std::string STR_SUFFIX_KTX(".ktx");

This makes program work..

std::string const STR_PATH_ASSET("assets/");
std::string const STR_PATH_SD("/sdcard/unlock_data/assets/");
std::string const STR_SUFFIX_PNG(".png");
std::string const STR_SUFFIX_KTX(".ktx");

It anyway works but I have no idea why this diffrerence results such strange behaviour. Any guess??


Added full source.

it was not only "std::string const" versus "const std::string" problem, but that declaration itself. sorry.

here is my source code. when i uncomment those std::string thing it does not work correctly. im drawing something onto my android but initial position of some mesh(vertices) differs when use that std::string thing. logically it must not affect wether this constants exists or not. im using ndk compiler version 4.6, ndk ver 14 on windows cygwin. will this const std::string declaration affect to another stack's memory? ie. transh value or something?

typedef enum _ImageCompressType{
    //REF http://stackoverflow.com/questions/9148795/android-opengl-texture-compression
    COMPRESS_UNAVAILABLE = -1,
    COMPRESS_ETC1 = 1,
    COMPRESS_PVRTC,
    COMPRESS_ATITC,
    COMPRESS_S3TC
}ImageCompressType;

typedef enum _FileDataFrom{
    FROM_ASSET, FROM_SD
}FileDataFrom;

//std::string const STR_PATH_ASSET("assets/");
//std::string const STR_PATH_SD("/sdcard/unlock_data/assets/");
//std::string const STR_SUFFIX_PNG(".png");
//std::string const STR_SUFFIX_KTX(".ktx");

class ImagesLoader {
public:
    ImagesLoader* mgr;

    static ImagesLoader* getInstance();
    static void destroyInstance();

    ImageCompressType TypeImgComrpess;

//  GLuint* loadTextures(FileDataFrom from, std::vector<std::string> filename);
private:
    ImagesLoader() {}
    ~ImagesLoader() {}

    static ImagesLoader* self;
};
4

1 回答 1

6

取自: http ://www.cprogramming.com/tutorial/const_correctness.html

声明 const 变量时,可以将 const 放在类型之前或之后

int const x = 5;

const int x = 4;

导致 x 是一个常数整数。

您提供的代码示例不是您遇到的“奇怪行为”的原因。

于 2013-07-16T09:49:42.513 回答