logo
down
shadow

Using input to call a member function


Using input to call a member function

By : user3849556
Date : October 22 2020, 11:12 PM
wish help you to fix your issue If all of your methods have the same number of arguments (or preferably, no arguments), then you can easily do this using an std::map.
Use the method name as key of the map, use a pointer to the method as value in the map. Then lookup the entered string in the map, and execute its corresponding method via the method pointer.
code :


Share : facebook icon twitter icon
Member function call on shared_ptr through member function pointer in template

Member function call on shared_ptr through member function pointer in template


By : Rodrigo Mateus Alves
Date : March 29 2020, 07:55 AM
may help you . std::shared_ptr doesn't support pointer-to-member access operator (i.e. ->* and .*). So we can't invoke member function pointers with ->* on it directly. You can change the invoking syntax to use operator* and operator.*, which works for both raw pointers and smart pointers.
code :
template <typename Ptr, typename MemberFunctor>
int example(Ptr ptr, MemberFunctor func )
{
    return ((*ptr).*func)();
}
How to make a reference of or call a member function if that member function exists

How to make a reference of or call a member function if that member function exists


By : user2781609
Date : March 29 2020, 07:55 AM
will help you It's not meant to be used like this. Reflection is something you use if you don't know at compile time what you are dealing with at runtime. Some examples:
you need to use a type that's configured in some properties file (Class.forName("someTypeString").newInstance()) you have written an utility that extracts the contents of your object for debugging purposes you need to access code that isn't really visible to you (private fields that you can't easily access, but you need to) many more... but most of the time very special use-cases
code :
fun canceller(object: Any): KFunction<Any>? {
  var canceller: KFunction<Any>? = null

  // check if object is of type Cancellable
  if (object is Cancellable) {
    // make reference of the Cancellable::cancel-function
    canceller = object::cancel  // smart-cast acting

    // or you could also call it directly: object.cancel()
  }
  return canceller
}
val someObj : Cancellable = ...
// somewhere later:
someObj.cancel()
object::class.members.first {
  // note: I am using just the first function... if there are several, you need to check which one to use (parameter/type)
          it.name == "cancel" 
   }
   .call(object)
How does the compiler call a Derived member function, when I use a member function pointer defined in terms of the Base

How does the compiler call a Derived member function, when I use a member function pointer defined in terms of the Base


By : RealLive Willem
Date : March 29 2020, 07:55 AM
help you fix your problem member_func() is a virtual function. The compiler will hence always call the most appropriate function for the real type of the object that is pointed to. The way this is ensured is implementation specific.
The most frequently way to do it is to use a vtable, a table of funtion pointers. A pointer to the vtable is initialised during the construction of the object. Every time you refer to such a virtual function, the compiler will generate code to find the function pointer in the vtable (using an ofset in the vtable). And when you use a pointer to a virtual function, the compiler will generate code to find the right pointer in the vtable.
call C++ member function from the string input format

call C++ member function from the string input format


By : grace
Date : March 29 2020, 07:55 AM
will be helpful for those in need You cannot dynamically modify C++ types. However, it sounds like you just want a way to set and read attributes. You don't need to modify your class structure for this, there are other alternative solutions. For example you could use an std::map:
Correct way to call a member function via a QHash with member function pointers

Correct way to call a member function via a QHash with member function pointers


By : AleksandarGledic
Date : October 05 2020, 03:00 PM
This might help you Your approach is reasonable, but you've changed the no-matches scenario from executing an else block (potentially doing nothing at all) to instant undefined behavior.
You need to separate the hash lookup from the call so you can insert a check for successful lookup in between. With C++ standard collections (std::map which is a red-black tree, std::unordered_map which is a hashtable), that'd be a call to find(key) which returns an iterator... you compare it to map.end() and make very sure not to dereference if they are equal. QHash, or any other non-standard hashtable, will surely provide something similar.
Related Posts Related Posts :
  • How to use priority_queue with a non-static compare method of class instance?
  • Template parameters inside and outside class/struct
  • Determining prime number
  • How to resolve ambiguity between constructors taking std::string and std::vector
  • My program crashes when I try to change private values from an object
  • Unordered_map with custom class as key
  • Strict aliasing rules broken with templates and inheritance
  • C++ Derived Class Override Return Type
  • singly linked list c++ constructor, destructor and printing out
  • How to clone class with vector of unique_ptr to base class
  • error: no match for operator
  • std::vector doesnt accept my struct as template
  • selection of people's contours
  • how to fix the (Error using mexOpenCV) on matlab?
  • Is or was there a proposal for c++ to use the context for short enum values?
  • Fair assumptions about std::hash implementations
  • undefined reference to libusb using cyusb
  • Function returns null pointer instead of address
  • C++17 copy elision and object destruction
  • Input multiple strings via operator>> c++
  • Avoiding overflow boost container
  • How to Write a Lambda Wrapping a Function with Optional Return Value
  • Partial specialization with more template parameters
  • How to convert fixed size array to pointer on pointer array
  • Memory leak in const member constructor with tag dispatching
  • C++ function with a generic and optional class
  • Custom QGraphicsItem That Contains Child QGraphicsItems
  • Are There Restrictions on What can be Passed to auto Template Parameters?
  • Rotating line inside rectangle bounds
  • Why do I need dynamic memory allocation if I can just create an array?
  • How can I convert a text file into a form that MPI_Bcast can send?
  • How to get array of all `this` of an instance
  • Using pointers as parameters
  • Automatic type deduction with const_cast is not working
  • Why does std::is_rvalue_reference not do what it is advertised to do?
  • Function Template Specialization with Forward Declared Type
  • template deduction failed in vector
  • Is there a signed `sizeof` alternative in Qt
  • clarification on overloading the -> operator
  • What is __m128d?
  • QtConcurrent: why releaseThread and reserveThread cause deadlock?
  • Function receiving different value than passed
  • Can C++ close a '''fstream''' variable after '''.close()'''?
  • Is it necessary to overload operator in this specific case
  • Comparing an element of a string array with a string
  • how to dereference a pointer of a map of pointers to objects in c++
  • How recursive function control flow change for static variable?
  • SDL 2 blitting BMP File
  • Why does an extremely large value cause this code to repeat infinitely?
  • Function returning different value than what is in function body before return statement
  • Struggling with including Headers in another header file (C++)
  • WebViewProcessControl initialization crash
  • C++ exception 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00C02F30)
  • What's difference between forward and move in the constructor of class?
  • C++: Is it possible to condense `bool` objects within the same byte?
  • I cant understand this access modifier
  • Boost Spirit X3: Collapsing one-element lists
  • How can I add a char to an istringstream?
  • Wrapping variadic templates in pybind11
  • C++ referencing instances created within a function's scope
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org