joshuajnoble wrote:
I'm not sure, I can't see major problems in this piece of code.
this is maybe val2[0] = "..."; otherwise, it won't compile. However, it
will give you warning anyway regarding the conversion from const char*
(the string literal) to char*.
Surely inst is initialised somewhere to NULL, as in
SingletonImpl* SingletonImpl:: inst = NULL;
or similar. Otherwise it won't link. If you don't initialise it to zero,
it will compile and crash.
again, only the deprecated casts from string literal to char* seem "wrong".
I don't think so, really. Araprt from the fact that the level here gets
very low at times, every question is worth discussing. Check the
SingletonImpl:: inst initialisation, and if it is all right, in my
opinion it should not crash, so you may want to post the complete code
that in your pc generates the error.
Best wishes,
Zeppe
I threw together a quick test here, I'm thinking my problem is in what
I'm doing with the static pointer (perhaps). Here's my Singleton:
I'm doing with the static pointer (perhaps). Here's my Singleton:
>
>
#ifndef SINGLETON
#define SINGLETON
>
class SingletonImpl
{
public:
static SingletonImpl* getInst();
int val1;
char* val2[5];
>
protected:
static SingletonImpl* inst;
SingletonImpl() ;
>
};
>
#endif
>
SingletonImpl:: SingletonImpl() {
val1 = 1;
val2 = "abcdefghijklmn ";
>
#ifndef SINGLETON
#define SINGLETON
>
class SingletonImpl
{
public:
static SingletonImpl* getInst();
int val1;
char* val2[5];
>
protected:
static SingletonImpl* inst;
SingletonImpl() ;
>
};
>
#endif
>
SingletonImpl:: SingletonImpl() {
val1 = 1;
val2 = "abcdefghijklmn ";
will give you warning anyway regarding the conversion from const char*
(the string literal) to char*.
}
>
SingletonImpl* SingletonImpl:: getInst(){
if(inst == 0) {
inst = new SingletonImpl() ;
}
return inst;
}
>
SingletonImpl* SingletonImpl:: getInst(){
if(inst == 0) {
inst = new SingletonImpl() ;
}
return inst;
}
SingletonImpl* SingletonImpl:: inst = NULL;
or similar. Otherwise it won't link. If you don't initialise it to zero,
it will compile and crash.
TestObjs::TestO bjs(char* ch){
c = ch;
SingletonImpl:: getInst()->val2[0] = c;
}
>
class TestObjs {
public:
TestObjs(char* ch);
char* c;
};
>
>
And then my main:
>
int main (int argc, char * const argv[]) {
char* c = "ghu";
TestObjs t = TestObjs(c);
char* c3 = "3333333";
TestObjs t2 = TestObjs(c3);
return 0;
}
c = ch;
SingletonImpl:: getInst()->val2[0] = c;
}
>
class TestObjs {
public:
TestObjs(char* ch);
char* c;
};
>
>
And then my main:
>
int main (int argc, char * const argv[]) {
char* c = "ghu";
TestObjs t = TestObjs(c);
char* c3 = "3333333";
TestObjs t2 = TestObjs(c3);
return 0;
}
And this blows up...I realize this is a pretty rudimentary question
and not really on par with the normal level of discussion here so
thanks in advance for any help and your patience :)
and not really on par with the normal level of discussion here so
thanks in advance for any help and your patience :)
very low at times, every question is worth discussing. Check the
SingletonImpl:: inst initialisation, and if it is all right, in my
opinion it should not crash, so you may want to post the complete code
that in your pc generates the error.
Best wishes,
Zeppe
Comment