logo
down
shadow

How do I use function pointers within this class?


How do I use function pointers within this class?

By : user2952921
Date : November 19 2020, 03:59 PM
Does that help From the error message, it appears that draw is a generic pointer to a function, and not a pointer to a member function. The declaration of draw should be void (X2D::GL::Circle::*draw)(void), so that draw points to a member function of Circle.
code :


Share : facebook icon twitter icon
Bad practice to use function pointers to members of a class T as parameters in functions of a template class<T>?

Bad practice to use function pointers to members of a class T as parameters in functions of a template class<T>?


By : M_M_Hasan
Date : March 29 2020, 07:55 AM
help you fix your problem I don't have any problem with your architecture - I don't see it as bad practice. To me it seems quite a laborious way to protect data and doesn't really help you much in that the user can use any void function to modify the contained data which isn;t really a contract on what can and can't be changed.
I think the reason this construct is so rarely seen is that your requirement and goals of the container class are unusual.
C++ calling a function from a vector of function pointers inside a class where the function definition is in main

C++ calling a function from a vector of function pointers inside a class where the function definition is in main


By : Nalin Gupta
Date : March 29 2020, 07:55 AM
Does that help Your code is almost correct. Your vector is mistakenly holding pointers to pointers to functions rather than simply pointers to functions. addThingy is adding the address of the function pointer in to the vector, but that pointer goes out of scope in the next line.
Change your code as follows:
code :
//Store pointers to functions, rather than
//pointers to pointers to functions
std::vector<void (*)()> vectoroffunctions;

SomeClass::addThingy(void (*function)())
{
    //Don't take the address of the address:
    vectoroffunctions.push_back(function);
}
c++ Creating vector of function pointers in a given class, with function being a member of a different class

c++ Creating vector of function pointers in a given class, with function being a member of a different class


By : PP2IT PP2IT
Date : March 29 2020, 07:55 AM
To fix the issue you can do A pointer to the non-static member function A::func doesn't have type void(*)(void), it has type void(A::*)(void). So either change the vector to match the elements that you want it to have, or else use a pointer to a function of the correct type for the vector.
Call templated function with derived class arguments using base class pointers

Call templated function with derived class arguments using base class pointers


By : WubTub Official
Date : March 29 2020, 07:55 AM
I wish this help you The guideline
Polymorphism should generally be preferred over switch statements which check for a type, because the resulting code is much more type-safe and you get compile-time errors instead of runtime-errors which is generally a good thing. You have the interesting case of a function which takes two objects and which should be dispatched depending dynamically on both argument types.
code :
class Box;
class Sphere;
class Cone;
// ...

class GeometryBase
{
public:
    virtual bool collidesWith( const GeometryBase & other ) const = 0;

protected:
    virtual bool dispatchCollidesWith( const Box    & other ) const = 0;
    virtual bool dispatchCollidesWith( const Sphere & other ) const = 0;
    virtual bool dispatchCollidesWith( const Cone   & other ) const = 0;
    // ...
};
template <typename T>
class GeometryImpl : public GeometryBase
{
public:
    virtual bool collidesWith( const GeometryBase & other ) const
    {
        assert( typeid(*this) == typeid(T) );
        return other.dispatchCollidesWith( static_cast<const T&>(*this) );
    }
};
class Box : public GeometryImpl<Box>
{
protected:
    virtual bool dispatchCollidesWith( const Box    & other ) const { /* do the math */ }
    virtual bool dispatchCollidesWith( const Sphere & other ) const { /* do the math */ }
    virtual bool dispatchCollidesWith( const Cone   & other ) const { /* do the math */ }
    // ...
private:
    // data ...
};
geom1.collidesWith( geom2 );
class GeometryDispatcher;

class GeometryBase
{
public:
    void run( GeometryDispatcher & dispatcher ) const = 0;
};

class GeometryDispatcher
{
public:
    virtual void dispatch( const Box    & shape ) = 0;
    virtual void dispatch( const Sphere & shape ) = 0;
    virtual void dispatch( const Cone   & shape ) = 0;
};
void Box::run( GeometryDispatcher & dispatcher ) const
{
    dispatcher.dispatch( *this );
}
class CollidesWithBoxDispatcher : public GeometryDispatcher
{
public:
    CollidesWithBoxDispatcher( const Box & box ) : box(box) {}

    bool getResult() const { return result; }

    virtual void dispatch( const Box    & shape ) { ... }
    virtual void dispatch( const Sphere & shape ) { ... }
    virtual void dispatch( const Cone   & shape ) { ... }

private:
    bool result;
    const Box & box;
};

bool collidesWithBox( const GeometryBase & shape, const Box & box )
{
    CollidesWithBoxDispatcher d( box );
    shape.run( d );
    return d.result;
}
class CollidesWithDispatcher : public GeometryDispatcher
{
public:
    CollidesWithDispatcher( const GeometryBase & shape ) : shape(shape) {}

    bool getResult() const { return result; }

    virtual void dispatch( const Box    & box    )
    { 
        result = collidesWithBox( shape, box );
    }

    virtual void dispatch( const Sphere & sphere ) { ... }
    virtual void dispatch( const Cone   & cone   ) { ... }

private:
    bool result;
    const GeometryBase & shape;
};

bool collidesWith( const GeometryBase & shape1, const GeometryBase shape2 )
{
    CollidesWithDispatcher d( shape2 );
    shape1.run( d );
    return d.result;
}
Any purpose of using base class pointers to derived class if virtual function is not used

Any purpose of using base class pointers to derived class if virtual function is not used


By : paynehunter
Date : March 29 2020, 07:55 AM
this one helps. Using a base class pointer or reference allows you to write functions that accept all derived classes of the base generically so you can call common non-virtual functions on them. Otherwise, you'd have to write the same function multiple times for the different derived classes, or use templates.
code :
#include <iostream>

class base_t {
public:
    void non_virtual() const { std::cout << "Hello, World!\n"; }
};

class derived_1_t : public base_t { };
class derived_2_t : public base_t { };

void do_something(const base_t& obj) {
    obj.non_virtual();
}

int main() {
    derived_1_t var1;
    derived_2_t var2;
    do_something(var1);
    do_something(var2);
}
Related Posts Related Posts :
  • What is the purpose of name mangling in C++?
  • C++ Why the void function is not working eventhough I called it?
  • How the pointer and the pointer's adress can share the same memory adress?
  • error C2220: warning treated as error - no 'object' file generated
  • exception of strcpy function in c++ console program
  • How to access USB barcode scanner data directly from USB port using C/C++ without driver
  • Under which circumstances will std::vector.clear() call a destructor?
  • Template partial specialisation and dependent names
  • best way to share data between c codes
  • C++ ignores if statement conditions
  • How to set the argv[ ] to be case-insensitive in a Win32 Console Application?
  • How to fix error "clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)"?
  • C++ How do I print a .txt file verbatim?
  • Creating two dimensional array of class
  • How do I correctly use COMMTIMEOUTS with OVERLAPPED IO mode reading from a Serial port
  • An assert macro which expands to static_assert when possible?
  • How to write a copyconstructor for a class which has HANDLE as a member in win32 c++ application?
  • C++ saving info such as tree in a file
  • Transforming an expression template tree
  • How to overload an operator with multiple parameters like a + b + c?
  • C++ 11 with Raspberry Pi performances
  • Make a C++ class look like a numpy array using swig
  • Postfix incrementer overloading for nested enumerated types
  • Is there a tidy way of associating metadata with functions in C++
  • QObject::installEventFilter(): Cannot filter events for objects in a different thread
  • LNK2005 error with Zxing C++
  • C++ Doubly Linked List with Pointers: Object of class isn't constructed properly
  • Using a random string generator in c++ constructor
  • What should I use instead of void as one of the alternative types in an variant?
  • C++ return value from multithreads using reference
  • How to connect multiple TCP IP clients to same server port using c++
  • Defaul compiler generates the reference operator (In C++)?
  • Unable to change directory time stamp after using FILE_FLAG_BACKUP_SEMANTICS
  • vector handling displaying output
  • WSAGetLastError returns WSAENOTSOCK - Cause?
  • C++: How to overload pow for user type?
  • C++ using arrays as multidimensional despite initalising it as 1D with pointer
  • How negate std::is_integral for use in tag dispatch?
  • Retrieve serial number from USB memory (Windows environment c++)
  • g++ error: invalid preprocessing directive #INCLUDE
  • C++ What is the std::for_each() function parameter type?
  • C++: Read individual lines from text file, sort words alphabetically
  • Saving 'this' address into a variable
  • c++ command line arguments in ubuntu terminal
  • Convert "Cartesian coordinates" to "polar coordinates with respect to user specified origin"
  • In what order are local scoped objects destructed?
  • How to use SDL_MapRGB with SDL 2.0
  • how compiler and interpreter work in case of array declaration
  • GSL integration behaves strange
  • Cropping an image with OpenCV and C
  • Find the last line in text file and select the first 10 char and print to a new file?
  • Created many CCSprits but when triggering ccTouchBegan gives the last one allways
  • seekp and seekg don't work with fstream
  • Taking input in Sublime Text 3
  • ld: -bind_at_load and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together
  • C++ storing #define as std::string would give size as zero compile time
  • How to use static linking with OpenSSL in C/C++
  • What is the difference between a trap, an error, a failure and program abortion?
  • Dynamic members allocation in qt
  • How to reduce object file size when compiling for VxWorks 5.5.1?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org