-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;
};
minimanimo
  • 185
  • 3
  • 11
  • 7
    It shouldn't make a difference. – Luchian Grigore Jul 16 '13 at 09:29
  • but it drives different result.. – minimanimo Jul 16 '13 at 09:36
  • There's something else involved. `const std::string stringName` is exactly the same as `std::string const stringname`. – James Kanze Jul 16 '13 at 09:39
  • no it doesn't, unless you have a very broken compiler. What were the differences in the results you were seeing? – Salgar Jul 16 '13 at 09:39
  • Try to reduce this to a small self-contained example that you could post (there is no #include so certainly you didn't post the whole program), and make sure no macro is involved. – Marc Glisse Jul 16 '13 at 09:48
  • Of course it doesn't work, you are missing a declaration of std::string (maybe missing #include ?). – Marc Glisse Jul 16 '13 at 10:12
  • How sure are you that the `STR_XXXXX` are not defined as macros? – Casey Jul 16 '13 at 16:42
  • endup with #define.. and been changed string name. don't know weather STR_... was already been there, but i suppose so. here is how story ends.. #define PREFIX_PATH_ASSET (std::string("assets/")) #define PREFIX_PATH_SD (std::string("/sdcard/unlock_data/assets/")) #define SUFFIX_EXTENSION_PNG (std::string(".png")) #define SUFFIX_EXTENSION_KTX (std::string(".ktx")) – minimanimo Jul 22 '13 at 05:46

1 Answers1

6

taken from: http://www.cprogramming.com/tutorial/const_correctness.html

When declaring a const variable, it is possible to put const either before or after the type

int const x = 5;

and

const int x = 4;

result in x's being a constant integer.

the code sample you provided is not the cause for the "strange behavior" you encountered.

Rayee Roded
  • 2,440
  • 1
  • 20
  • 21