I'm trying to make a game so I need some kind of external header file for all constants, static fields and such. It's just a header file without a class,so I can use all its fields from any class. But when I try to add static pointer fields to my header file something strange happens. If I try to set up those pointers with NEW CLASSNAME() from any class they work only in this class. From anywhere other then that class pointer stays Null.
I tryed to use not static pointer field and shared_ptr.
I need to have an external pointer field I can always get or reset with new, from anywhere.
(Don't think if this matters but the pointer is to my own class)
Related
I have several classes that all inherit the same interface. These classes are instantiated and saved in interface pointers. For example:
struct Class {
private:
Function* function;
};
I need to save the function to a file, and am wondering if saving the function as (*function), I will need to add an enum or do instance checking? The function objects contain one method that needs to be called from the abstract function class after loading the file and I want to make sure I am able to call that function after loading the file.
The only thing you can do: Make a list of all the function pointers that you would want to store, assign an unchangeable unique number to each, write that number to the file, and upon reading, replace it with the function pointer.
I am working on an assignment, and while I don't have any issues with the actual assignment, I want to make my code "proper." I am trying to define a static variable in my header file. I want it to be static so that all instances of this class can access the same variable. I don't want to extern it either, I want just this class to have access to it.Then I am trying to define it in the actual file (not header) but I keep getting an error about not being able to cast. I just want to learn how to properly do this to have cleaner code.
The definition of your field stays inside the class:
class AddrSpace
{
List *availSpots;
};
The full name of this variable will be AddrSpace::availSpots. C++ requires to define static variables (including the static fields of the classes) explicitly. I would not say this is fully logical because compiler already has everything to generate all necessary stuffs. Nevertheless this is so. The definition in your C++ file should look like:
List *AddrSpace::availSpots = new List();
Next time please add the source directly into the question.
I have a an instance of lasse1 and I want to use it in a method of lasse2 , this method is static method, this just doesn't work :
class Lasse2{
......
public :
static void function(void);
Lasse1* obj;
........
};
And now i want to use it like :
void Lasse2::function(void){
obj->dosmt(); // this doesn't work
.........
any idea how can I solve this?
If you want to access an instance member of your class, then you must have an instance of that class. There's no way around this. Your options are:
Make obj a static member. Do this if you intend to have a single obj for all instances of this class.
Remove static from function() so it becomes an instance method.
If you can't do either of those, then you need to find a way to pass an instance pointer to your function. For example, APIs that require a function pointer often have a mechanism for passing pointer-sized data to that function when it's eventually called.
Change your static method to explicitly pass the object pointer:
static void function(Lasse1* obj)
{
obj->dosmt();
}
But before you do, consider what you're really trying to do (and even write another question if you like).
You need an instance of your class to pull that off.
Create one or receive it through other means (function argument, global variable, class static variable, etc)
SLaks said it best: "You can't"
Here's why:
When you declare a member variable (not static, see obj above), you're telling the compiler that each object of type Lassie2 contains a pointer to a Lassie1.
When you declare a method static, that means that it is independent of all the instances (the actual objects) of that class. It doesn't operate on an object.
So inside of Lasse2::function, there's no this, no Lassie2 object for you to get the obj pointer from.
I'm using MFC's CArhive class to save off a project file for my app. One of the class names is wildly inaccurate and I'd like to change it, but simply changing the name everywhere renders previous archive files useless with an archive error. Is there a way to change the name of the archived class without rendering all previously saved files useless?
That is of course without using a typedef to access the existing class with a new name or keeping around a version of the class with the old name to read archived files and copying the read in objects to the same class with a new name.
The crucial point is that when you use DECLARE_SERIAL and IMPLEMENT_SERIAL, a CRuntimeClass member is added to your class containing the name in its m_lpszClassName field.
This CRuntimeClass object is also added to a list maintained by the framework that is searched when classes are dynamically created.
You need to make sure that the CRuntimeClass object contains the old name of your class in m_lpszClassName.
The two options you have are:
Override the construction of the
CRuntimeClass object to set the name
Change the class name stored in its
m_lpszClassName field after it has
been created
Overriding the construction of the CRuntimeClass object
To do this, you will need to make your own versions of DECLARE_DYMAMIC, DECLARE_DYNCREATE, DECLARE_SERIAL, IMPLEMENT_DYMAMIC, IMPLEMENT_DYNCREATE, and IMPLEMENT_SERIAL.
You can just copy and rename the existing implementations.
In your version of IMPLEMENT_DYNAMIC, you need to change the code that constructs the CRuntimeClass so that it is initialised with the old class's name.
Changing the class name stored in the m_lpszClassName field after the CRuntimeClass object has been created
As the CRuntimeClass is created by a static initializer, I don't think that you can do this from within your class.
I think that the best place to do it is in your application's InitInstance.
Add a static char* variable to your application's class containing the old class name.
Then, in InitInstance, set the m_lpszClassName field in your class's CRuntimeClass.
The first method has the advantage of
keeping changes within the class
itself.
The second makes the application
aware of the class in a way that it
possibly shouldn't be.
Either way, the first thing to do is to completely familiarise yourself with the way dynamic creation and serialization work.
There is a 3rd option which I took. I originally named one of my serializable classes CEvent which didn't seem to be a problem on VC6.0; but on VS2010 there is a MFC built-in class called CEvent so I had to change it.
So, I fixed it by modifying old format files before calling COleServerDoc::OnOpenDocument
I simply, open, read to memory, find and replace CEvent with any other 6 character (has to be same size) class name, write and save as new filename, then open and read new file, no problem.
I'm changing file extension for the new version anyway, so it all works out.
I'm using Boost.Property_Tree for a project and I want to add a small bit of functionality to it. I want to add a "fromFile" static member variable that will figure out the file type and then use the proper parser. In my project, this is currently how I've got it.
typedef boost::property_tree::ptree ConfigNode;
Then I have another class called ConfigLoader that I've been using to load the file. I want to add the one function to a ConfigNode class though. I need the compiler to treat ConfigNode as if it were a boost::property_tree, but I want to add one static function. Is there a way to do this?
Nope. There's no clean way to do this.
You have two options:
Declare your functions and variables outside of the class completely (e.g. in another class or global in some namespace).
Subclass boost::property_tree, adding your static member(s).