473,383 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

types of a pointer

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
Oct 3 '05 #1
6 1502
Nico Kruithof wrote:
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-

Oct 3 '05 #2
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

Shezan Baig wrote:
Nico Kruithof wrote:
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-

Oct 3 '05 #3

Nico Kruithof wrote:
Shezan Baig wrote:
Nico Kruithof wrote:
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-

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

Oct 3 '05 #4
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

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

Oct 3 '05 #5
Nico Kruithof wrote:
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-

Oct 3 '05 #6
Yes I see. Thank you for your help. I will assume that the pointer
refines the STL container concept.

Nico
Shezan Baig wrote:
Nico Kruithof wrote:
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-

Oct 3 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
10
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
58
by: jacob navia | last post by:
Hi people I have been working again in my tutorial, and I have finished the "types" chapter. If you feel like "jacob bashing" this is the occasion! I am asking for criticisms, or for things I may...
5
by: mike.polyakov | last post by:
I have trouble understanding incomplete types and their interplay with shared_ptr. Consider the following code, composed of two source files and one header: ...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.