pointer points to a member class

Well, I am looking for a way to find out wheter my pointer is pointing to a member of a particular class

and somehow I figure out a way by myself...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>

using namespace std;

struct example_t {
    example_t(){
        p = q = 0;
    }

    int p;
    int q;
};

int main() {
    vector< example_t * > datax;

    datax.push_back( new example_t() );
    datax.push_back( new example_t() );
    datax.push_back( new example_t() );
    datax.push_back( new example_t() );
    datax.push_back( new example_t() );

    int * a = &datax[3]->p;

    for( int i = 0; i < datax.size(); ++i ){
        if( ( void * ) datax[i] <= ( void * )  a && ( void * ) a < ( void * ) datax[i] + sizeof( example_t ) ){
            cout << i << endl;
            break;
        }
    }

    return 0;
}


Just thinking is this good pratice or is there better ways to do this ??
Last edited on
To make it meaningful, use char*, not void* (how are you even adding a number to a void*? It doesn't compile on anything I tried, and doesn't make sense logically).

It isn't good practice to find yourself in a situation that requires something like this, but if you are, i think this is sound (except on those rare systems where operator< for pointers does not provide total ordering, so if you want to be paranoid, use std::less instead)

(PS: don't forget to use delete or appropriate smart pointers/containers)
Last edited on
How could it be rare ?
Well this is the first time I need this kind of strange checking...

I guess I'll use char*

Well, I can change the code to where I don't need this but I am afraid it may complecate the class....


It isn't good practice to find yourself in a situation that requires something like this


Why is that so ?
Last edited on
If the number of member variables is not very large, this is an option:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <memory>
#include <vector>
#include <iostream>

struct R { R() {} /* ... */ } ;

struct example_t
{
    example_t() : p(0), q(0) {}

    int p ;
    int q ;
    R r ;

    bool points_to_member( const int* pointer ) const
    { return ( pointer == &p ) || ( pointer == &q ) ; }

    bool points_to_member( const R* pointer ) const
    { return pointer == std::addressof(r) ; }
};

int main()
{
    std::vector< std::unique_ptr<example_t> > datax ;
    for( int i = 0 ; i < 10 ; ++i ) datax.emplace_back( new example_t() ) ;

    int* a = &datax[3]->p;

    for( std::size_t i = 0; i < datax.size(); ++i )
    {
        if( datax[i]->points_to_member(a) )
        {
            std::cout << i << '\n' ;
            break ;
        }
    }
}

What if it's large... ?

My Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct example_t
{
    example_t() : p(0), q(0) {}

    int p ;
    int q ;
    R r ;

    template< class T >
    bool points_to_member( const T * ptr ) const {
        return ( (char *)(this) <= (char *)ptr && (char *)ptr < (char *)(this) + sizeof( this ) );
    }
};


It's just a cleaner version from what I did before


I try to avoid C++ casting here
I haven't read much about static_cast and dynamic_cast
So I better not use it in all the wrong places


Maybe I should just go with "change the class so that I don't need to do this ..."

So which is better change the class or perform this kind of check

Tell me your opinion...
Last edited on
return ( (char *)(this) <= (char *)ptr && (char *)ptr < (char *)(this) + sizeof( *this ) );

> I try to avoid C++ casting here

These casts are reinterpret_casts


> So which is better change the class or perform this kind of check?

In general, it is best to try and avoid this kind of check. Hard to say anything more without knowing the context: why is this kind of check needed?
So I making an animation engine for my game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct tween_t;
struct tween_t;

struct world_t {
      list< unique_ptr<object_t> > Objects;
      list< unique_ptr<tween_t> > Tweens;
/* more functions here */
} world_1;

template< class T >
struct tween_t {
    tween_t( T& element, T dd, double duration, .... );
    /* more functions here */
};

struct object_t {
	double x,y;
	double rot;
	sf::IntRect Rect;
	sf::Color Color;
	image_t * image;
        world_t * myworld;

        int moveTo(){
            myworld->Tweens.push_back( new tween_t<double>(x,100.0, 2.0,... ) );
        }         
};


I think this is sufficient
My code is too long to be pasted here

When the Objects get destructed because event cause by player
and somehow There is some animation that haven't ended
and still being handling a data to a member of class that doesn't exist anymore

Well I could just put the tween_t handler not in the world_t but in the object_t
And Yeah I will now right away know every tween that handling what object

So what do you think
> Well I could just put the tween_t handler not in the world_t but in the object_t

That is one option.

Or a tween_t could have a back_pointer to the owning object_t:
1
2
3
4
5
6
7
8
struct tween_t
{
   // ...

   tween_t( /* ... */ object_t * o ) /* ... */ owner(o) { /* ... */ } 

   object_t* owner ;
};


1
2
3
int object_t::moveTo(){
            myworld->Tweens.push_back( new tween_t<double>(x,100.0, 2.0,..., this /* owner */  ) );
        }    
Topic archived. No new replies allowed.