What replace this in a Static Method? - list

I'm studying Java, i have to create a Static equal method in a generic class....in a non static class i use this. to refer to the object who call the equals, but in a static method how can i do?
> public static <T> boolean equals2(T object) {
if (GenericClass == object) return true;
if (object == null || GenericClass.class != object.getClass()) return false;
GenericClass<?> that = (GenericClass<?>) object;
return Objects.equals(object, that.t);
}
I'm trying this but it gave me error on generic Class

public static boolean equals2(T object) {
That method signature doesn't make sense because you only have 1 object so there isn't anything to compare it to. If that needs to be a static method then it would make more sense to have the equals2 method accept the 2 objects that you want to compare. There are many utility methods that do this sort of thing. One is https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Objects.html#equals(java.lang.Object,java.lang.Object).

The signature of the function should be different, you should get two objects
The first of type T
And the other of the object type.

Related

Static initialization of function pointers

I'm having a class that contains a function pointer. I would like to initialize various instances of the class statically but I can't figure out the correct syntax for this.
Let's say, this is my class
class fooClass
{
int theToken;
string theOutput;
bool (*theDefault)( void );
};
I now would like to create a static instance of this, like this…
fooClass test
{
1,
"Welcome",
(){ return (theToken & 1 ) ? true : false; }
};
As I said, I can't figure out the proper syntax for the function pointer line. Or is it even possible like this? I'd really like not having to break out every function I create this way into its own function declaration.
What I'm trying to do is, allow each instance to have a unique default function because each instance represents a unique data-driven building block of a bigger system. The code I put in there is just for illustrative purposes. This default function will access certain global variables as well as some of the member variables and if need be I could pass this into the function.
Could someone point me in the right direction how I'd have to write the initialization for it to work under C++14?
If you want to refer to struct members inside the function, you cannot do with just a plain function pointer not receiving any argument, as it doesn't receive the this pointer.
My advice is to at very least change it to a pointer to a function taking the instance as an argument, then in initialization you can pass a capture-less lambda (which can be converted to a plain function pointer):
class fooClass
{
int theToken;
string theOutput;
bool (*theDefault)( fooClass *that);
// you may provide a helper for ease of use
bool Default() { return theDefault(this);}
};
fooClass test
{
1,
"Welcome",
[] (fooClass *that){ return (that->theToken & 1 ) ? true : false; }
};
You can also use an std::function<bool(fooClass*)> to allow even functors, lambdas with captures & co. if you are ok with the increased overhead.
You may be tempted to use a plain std::function<bool()> instead, and use a lambda capturing the instance by reference, such as
fooClass test
{
1,
"Welcome",
[&test] (){ return (test->theToken & 1 ) ? true : false; }
};
This does work, but is extremely dangerous if test happens to be copied, as theDefault will still refer to test even in the copy (and even after the original will have been destroyed).
(incidentally, this is how OOP is often done in languages such as Lua, but there (1) objects are not copied and (2) automatic memory management makes sure that closures "keep alive" the objects they capture)

C++ static non-member function returning object of a template class

I have a static non-member function which returns a template class object depending on the type of the object.
template< typename T >
class Example
{
....
};
static Example non_member_function(int var) {
if (var == 1)
return Example<float>;
else
return Example<int>
}
This is not working as return type Example is a template. How can I write this return type
You cannot use different types in the return value without making the function a template too - each return type defines a new function, they are all different.
The better approach is to return a pointer, which does allow polymorphism.
Note though that you are then returning a pointer to a local object, which is undefined after the function ends. You would need to return a new object (return new Example<float>;), or make the two objects static inside the function and return their addresses -depending if you want to return each time a new object, or always the same one.
It doesn't really work like that - the compiler needs to know what the return type is, and you're returning either Example <int> or Example <float> (which are different types) depending on a variable passed in at runtime.
If you know what type you want at compile time, you can do this:
template <typename T> static Example<T> non_member_function() {
return Example<T> ();
}
And then call it like this:
Example <int> example1 = non_member_function <int> ();
or
Example <float> example2 = non_member_function <float> ();
C++ does not (directly) allow what you're trying to do. Example<float> and Example<int> are unrelated classes that do not necessarily have anything at all in common. So you can't have a single function return two different things, any more than you could write a function that sometimes returns a double and sometimes returns a std::vector<int>.
There are some ways to do similar things, but which is appropriate really depends on why you want the function to act this way and what you intend to do with the returned values.

How assign "this" object to a static class pointer

I am trying to assign the instance class to a pointer, how can I do that:
#include <fstream>
using namespace std;
class A
{
private:
static A *pointer;
protected:
A(){}
A GetInstance()
{
//On this point throw the error: could not convert A::pointer’ from A*’ to ‘A’
pointer& = (A)this;
return pointer;
}
};
A *A::pointer = NULL;
How can I do that?
static A *pointer;
...
...
pointer& = (A)this;
Is it possible? I am trying to create a simple repository class with an unique connection instantiated (only one time) and the class repository with the virtuals functions (Add, Save, GetById, GetAll()) inherit from this class, in this way I could create a lot of Repository classes using just only connection without open and close every time. If someone has a example, I would be glad.
Thanks in advanced.
It doesn't work as you tried because (A)this is a conversion from type A * (a pointer to an object of class A) to class A (an object of class A). The compiler cannot do such a conversion.
The type of A::pointer is A *. So is this inside the methods of class A.
All you need is:
pointer = this;
Or, if you want to make more readable, you can write:
A::pointer = this;
This way it's clear for a reader that pointer is a static property (class member) and not an instance property.
You have an error on the next line. The value returned by A::getInstance() (return pointer;) doesn't match the type declared in the method's header (an object of class A). The one that is wrong is the method's declaration. It doesn't make much sense to return an object of type A, it should return a pointer to such an object (more specifically, the only instance of the class, stored in A::pointer.
Change the method's definition like this:
A *GetInstance()
{
pointer = this;
return pointer;
}
As other readers already noticed in comments, it seems you are trying to implement the Singleton pattern (but you are only half-way through it). The best advice about Singleton is to avoid it because it is not testable. It is just a disguised global variable and global variables are, in general, a bad way to store your data.

Calling a non-static variable from a static function

I encountered an issue while trying to do something in the process of learning C++ and I am not sure how to handle the situation:
class Command
{
public:
const char * Name;
uint32 Permission;
bool (*Handler)(EmpH*, const char* args); // I do not want to change this by adding more arguments
};
class MyClass : public CommandScript
{
public:
MyClass() : CommandScript("listscript") { }
bool isActive = false;
Command* GetCommands() const
{
static Command commandtable[] =
{
{ "showlist", 3, &DoShowlistCommand } // Maybe handle that differently to fix the problem I've mentioned below?
};
return commandtable;
}
static bool DoShowlistCommand(EmpH * handler, const char * args)
{
// I need to use isActive here for IF statements but I cannot because
// DoShowlistCommand is static and isActive is not static.
// I cannot pass it as a parameter either because I do not want to
// change the structure of class Command at all
// Is there a way to do it?
}
};
Any help would be greatly appreciated! :)
// Is there a way to do it?
No.
Either pass it as parameter, make it static, or make DoShowlistCommand non-static.
There are two potential answers here:
1. about use of non static items in a static functions:
As said in our previous question/answer, this is not possible, unless you'd have in the static function a specific MyClass object (and use object.isActive). Unfortunately, you can't do this here :
your code comments clearly show that you can't add a MyClass parameter to the function call;
the existing parameters don't suggest that you have already a pointer to parent class object;
it would not be adivsable to use global objects in such a context.
2. about what your're trying to do:
It seems that you want to have the function static, because you want to provide it in a table that maps script-commands to function pointers.
Alternative A
If all the function pointers used in commandtable are members of MyClass, you could think of using a pointer to a member function instead of a pointer to a function. The outside object/function that sets isActive on an object, could then refer the pointer to the member function, on the MyClass object it knows.
Alternative B
Revise the design of your code to implement your script engine by using the command design pattern: it's ideally suited for this kind of problems. It will require some refactoring of your code, but it will be so much more maintenable and extensible afterwards !
I don't think there is any way to do it. Here is why:
A static member function is not attached to any particular object, which means it cannot access other members that are not static, since they are attached to an object.
It doesn't look like you need to make it a static member. If you are sure you do - then pass it as a parameter. For example, make a
bool isActive();
function, and pass an argument from it to that function somewhere when you call this 'problematic' one.
You also could change your member variable to static, but it looks like you need it for EACH object, not one-for-all

Issue with Item System regarding Inheritance

I had a question regarding how to solve the following in C++:
So, say I have 2 items. A Sword, and a Knife.
A sword's structure looks like the following:
baseItem > Equippable (Holds events and boolean checks) > Weapon (Same) > Sword
A knife's structure looks like the following:
baseItem > Equippable > Weapon > Knife
Most of my classes and functions that will be dealing with items (Inventory, Containers, the function that creates the Item Instances) will all be of type baseItem.
How do I specify functions such as:
baseItem createItem(int index, type itemType)
in such a way that I can return or cast back up to Sword/Knife? This is especially a concern with inventories as I will need to pull items from ItemSlot which will also hold objects of type baseItem, but will need to constantly check if they are Sword, Knife, etc.
You can do it with a template member function, like this:
class baseItem {
... // Private members
public:
template <typename T> static T* createItem(int index);
};
Then you will be able to call this with the exact type in a type parameter, like this:
Sword *sword = basrItem::createItem<Sword>(123);
Demo on ideone.
A couple of notes:
You need to return by pointer, regular or "smart", to avoid object slicing.
You may want to move the createItem member to a separate "factory" / "registry" class
If you keep a registry, you need to be careful about the ownership of your objects (i.e. avoid deleting them when they are inside the registry, or when they go out of registry while being in use elsewhere).
To illustrate the last point, here is what I mean by a registry class:
class baseItemRegistry {
map<int,unique_ptr<baseItem> > registry;
public:
template <typename T> static T* createItem(int index) {
map<int,baseItem*>::const_iterator iter = registry.find(index);
if (iter != registry.end()) {
return dynamic_cast<T*>(iter->second);
}
T* res = new T(index);
registry[index] = res;
return res;
}
};
This would let you keep a baseItemRegistry object that you can decide to share, but you could also hide it it you need a "private" registry. static functions do not give you this flexibility.
You use dynamic cast to cast to the derived type and check whether it is of that type. If you return the type by value you run into the slicing problem: What is object slicing?
You could return a pointer (preferably wrapped up in some kind of smart pointer) e.g. shared_ptr:
std::shared_ptr<baseItem> createItem(int index, type itemType)
{
// Your logic
return std::shared_ptr<baseItem>(new Sword()); // or whatever
} // eo createItem
And later on your cast to the appropriate type using dynamic_cast.
Generally you may cast baseItem to knife or sword with dynamic_cast, but that's not a good practice. Basically all behavior that depends on type of object should be located in the virtual member-functions of your classes.