On Oct 21, 3:27 pm, joshuajnoble <joshuajno...@g mail.comwrote:
[...]
This isn't the actual code, since it won't compile...
since this statement is illegal.
There's also the problem that you never defined
SingletonImpl:: inst. Which means undefined behavior---on my
machine, it won't link.
It doesn't compile, so it can't blow up. What else did you
change when posting, which might be hiding the problem? (The
basic singleton management seems perfectly correct, modulo the
missing data definition. So the problem has to do with
something you've accidentally changed, in the same way you
accidentally changed something which made it illegal, or with
the undefined behavior because of the missing definition of
SingletonImpl:: impl---maybe your compiler provides it
implicitly, but doesn't zero initialize it.)
--
James Kanze (GABI Software) email:james.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Oct 20, 12:03 pm, 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:
in what 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 ";
#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 ";
}
>
SingletonImpl* SingletonImpl:: getInst(){
if(inst == 0) {
inst = new SingletonImpl() ;
}
return inst;
}
>
SingletonImpl* SingletonImpl:: getInst(){
if(inst == 0) {
inst = new SingletonImpl() ;
}
return inst;
}
And then I have some objects that I need to have access the
singleton in their constructors:
singleton in their constructors:
#include "TestObjs.h "
TestObjs::TestO bjs(char* ch){
c = ch;
SingletonImpl:: getInst()->val2[0] = c;
>
}
c = ch;
SingletonImpl:: getInst()->val2[0] = c;
>
}
class TestObjs {
public:
TestObjs(char* ch);
char* c;
};
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;
}
char* c = "ghu";
TestObjs t = TestObjs(c);
char* c3 = "3333333";
TestObjs t2 = TestObjs(c3);
return 0;
}
SingletonImpl:: inst. Which means undefined behavior---on my
machine, it won't link.
And this blows up...
change when posting, which might be hiding the problem? (The
basic singleton management seems perfectly correct, modulo the
missing data definition. So the problem has to do with
something you've accidentally changed, in the same way you
accidentally changed something which made it illegal, or with
the undefined behavior because of the missing definition of
SingletonImpl:: impl---maybe your compiler provides it
implicitly, but doesn't zero initialize it.)
--
James Kanze (GABI Software) email:james.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34