types of a pointer

N

Nico Kruithof

Hello,

I have the following problem. Assume I have a templated class (B) that
is parameterized by a pointer to another class (A), how can I obtain
information of the types of A within B?

This small program might help:

class A {
public:
typedef int myInt;
};

template <class Ap>
class B {
public:
typedef Ap::myInt myInt; // ???????????
};

int main() {
B<A*> b;
}


Thanks in advance,
Nico
 
S

Shezan Baig

Nico said:
Hello,

I have the following problem. Assume I have a templated class (B) that
is parameterized by a pointer to another class (A), how can I obtain
information of the types of A within B?

This small program might help:

class A {
public:
typedef int myInt;
};


template <typename T>
struct RemovePointer {
typedef T Type;
};

template <typename T>
struct RemovePointer<T*> {
typedef T Type;
};

template <class Ap>
class B {
public:


typedef typename RemovePointer<Ap>::Type A;

typedef typename A::myInt myInt;
};

int main() {
B<A*> b;
}


Hope this helps,
-shez-
 
N

Nico Kruithof

It certainly does.

So the compiler matches the first definition in case of B<A> and the
second definition in case of B<A*>? Is there also a way to do this
without the additional struct? I was looking for the equivalent of '.'
and '->' for variables (and methods), but then for types.

Thanks,
Nico
 
G

Greg

Nico said:
It certainly does.

So the compiler matches the first definition in case of B<A> and the
second definition in case of B<A*>? Is there also a way to do this
without the additional struct? I was looking for the equivalent of '.'
and '->' for variables (and methods), but then for types.

Thanks,
Nico

If you are asking what B can discover about A's methods and member
variables (assuming A is a class) the answer is not very much. While
there are tricky ways for B to find whether A has a method with a
particular name, they are not straightforward. And I know of no way for
B to enumerate A's methods.

It is possible to declare pointers to A's methods and data members,
such as

int A::*p

is a pointer to a data member of A that is an int. While

long (A::*pmf)(char *)

is a pointer to a method in A that accepts a char * and returns a long.
Note that p and pmf are legal (but useless) if no such data member or
method actually exists in A. The template can also call A's methods by
name, but an error will occur if no such name exists in A.

Greg
 
N

Nico Kruithof

My question was not on methods and variables, but on types. If I have a
class A with variable v then it is possible to access it via:

A a;
a.v

or with pointers:

A *a;
a->v

This I know. Now, if I know that A has a type T then I can typedef is as
follows:
typedef A::T T;

However, if I have a type that is a pointer to A:
typedef A* Ap;

Is it then still possible to somehow obtain the type T from Ap?
typedef Ap::T T;

The method described earlier in this thread works, but uses an
additional class. I wondered wether his is possible without the
additional class. The reason is that my pointer is not really a pointer,
but a container that defines the operator*.

Best regards,
Nico
 
S

Shezan Baig

Nico said:
The method described earlier in this thread works, but uses an
additional class. I wondered wether his is possible without the
additional class. The reason is that my pointer is not really a pointer,
but a container that defines the operator*.


Then the container should contain the typedef. C++ standard library
classes do this a lot, e.g.:

typedef typename std::vector<T>::value_type VT;

-shez-
 
N

Nico Kruithof

Yes I see. Thank you for your help. I will assume that the pointer
refines the STL container concept.

Nico
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top