About pointer assignment [modified]
-
a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"
modified on Thursday, April 15, 2010 9:54 PM
-
a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"
modified on Thursday, April 15, 2010 9:54 PM
In a.cpp, try:
ClassOne *one = this;
What was I thinking?
modified on Thursday, April 15, 2010 10:44 PM
-
a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"
modified on Thursday, April 15, 2010 9:54 PM
If I understand what you are asking, then it should be something like this: - a.h
class ClassOne {
//stuff
void SetRef();
//more stuff
};
static ClassOne* one;- a.cpp
one(0);
inline void ClassOne::SetRef() {one = this;}Your problems are: - "this" is only valid inside class member methods. - You do not need to have "ClassOne::" infront of "one" unless it is a static member of the class (it is declared within the scope of the class). If "one" is a static member, this a.cpp would be as follows:
ClassOne * ClassOne::one(0);
inline void ClassOne::SetRef() {ClassOne::one = this;} -
a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"
modified on Thursday, April 15, 2010 9:54 PM
It seems you are confusing classes with instances (that is the same as confusing types with variables) local and global scope and linkage. Assuming the declaration you provided are at file scope (that is, not enclosed in some other braces)
one
is a pointer to a ClassOne object. It exist at global level (the pointer!) and have only local visibility (local in respect to the cpp file that "sees" it, that are all the cpp files that include the h file - in other words,static
has very few sense in h files)ClassOne::one
does not exist, sinceone
is global, and not a part ofClassOne
(hence the compiler doesn't know what to do), and than,this
is meaningless since you are not inside a member function body. At this point we cannot correct, since we cannot figure out what you where trying to do.2 bugs found. > recompile ... 65534 bugs found. :doh:
-
a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"
modified on Thursday, April 15, 2010 9:54 PM