How to avoid this typedef?

M

Martin Gieseking

Hello

I've the following piece of code that compiles fine:

typedef int (*(*T)[3])[4];
T *t = new T[k];

Now I would like to avoid the typedef but don't really know how to do this.
Is it possible at all?

Any help is really appreciated.

Alex
 
K

Karl Heinz Buchegger

Martin said:
Hello

I've the following piece of code that compiles fine:

typedef int (*(*T)[3])[4];
T *t = new T[k];

Now I would like to avoid the typedef but don't really know how to do this.
Is it possible at all?

It is possible. But why would you want to do it?
The code doesn't get clearer or simpler by doing so.
 
M

Martin Gieseking

Karl said:
I've the following piece of code that compiles fine:

typedef int (*(*T)[3])[4];
T *t = new T[k];

Now I would like to avoid the typedef but don't really know how to do
this. Is it possible at all?

It is possible. But why would you want to do it?
The code doesn't get clearer or simpler by doing so.

Because I have to create lots of dynamic arrays with various compound types
like those above. I don't want to add typedefs for all of them. Ok, maybe
the code becomes less readable but in these special cases I can live with
that.
 
T

tom_usenet

Hello

I've the following piece of code that compiles fine:

typedef int (*(*T)[3])[4];
T *t = new T[k];

Now I would like to avoid the typedef but don't really know how to do this.
Is it possible at all?

Don't do it!
int (*(*t)[3])[4] = new (int (*[10][3])[4]);
You have the choice of 1 nasty line and 1 simple line, or the above
completely hideous line.

I have to ask why you have such strange types anyway - containers
might provide a better approach, depending on your requirements.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
K

Karl Heinz Buchegger

Martin said:
I've the following piece of code that compiles fine:

typedef int (*(*T)[3])[4];
T *t = new T[k];

Now I would like to avoid the typedef but don't really know how to do
this. Is it possible at all?

It is possible. But why would you want to do it?
The code doesn't get clearer or simpler by doing so.

Because I have to create lots of dynamic arrays with various compound types
like those above. I don't want to add typedefs for all of them. Ok, maybe
the code becomes less readable but in these special cases I can live with
that.

If you think this is better:

void foo( int k )
{
int (*(* *t )[3])[4];

t = new ( int (*(*[k])[3])[4] ) ;
}

int main()
{
foo( 5);
}

But your description suggests, that your design needs some polish.
 
N

Nick Hounsome

Martin Gieseking said:
Karl said:
I've the following piece of code that compiles fine:

typedef int (*(*T)[3])[4];
T *t = new T[k];

Now I would like to avoid the typedef but don't really know how to do
this. Is it possible at all?

It is possible. But why would you want to do it?
The code doesn't get clearer or simpler by doing so.

Because I have to create lots of dynamic arrays with various compound types
like those above. I don't want to add typedefs for all of them. Ok, maybe
the code becomes less readable but in these special cases I can live with
that.

I'm not sure what sort of variation you are intending but templates might
make it simpler
(i'm not sure you wont need a typename in here somewhere):

template <typename T,int D1,int D2>
struct Confusion
{
typedef T* (*(*TYPE)[D!])[D2];
static TYPE* make(int n) { return new TYPE[n]; }
};

Confusion<T,3,4>::TYPE* x = Confusion<T,3,4>::make(42);

This at least gets rid of all the brackets.
 
M

Martijn Lievaart

template <typename T,int D1,int D2>
struct Confusion
{
typedef T* (*(*TYPE)[D!])[D2];
static TYPE* make(int n) { return new TYPE[n]; }
};

Confusion<T,3,4>::TYPE* x = Confusion<T,3,4>::make(42);

This at least gets rid of all the brackets.

Good thinking. I would do:

template <typename T,int D1,int D2>
struct Confusion
{
typedef T* (*(*type)[D1])[D2];
};

Confusion<T,3,4>::type *x = new Confusion<T,3,4>::type(42);

HTH,
M4
 
N

Nick Hounsome

Martijn Lievaart said:
template <typename T,int D1,int D2>
struct Confusion
{
typedef T* (*(*TYPE)[D!])[D2];
static TYPE* make(int n) { return new TYPE[n]; }
};

Confusion<T,3,4>::TYPE* x = Confusion<T,3,4>::make(42);

This at least gets rid of all the brackets.

Good thinking. I would do:

template <typename T,int D1,int D2>
struct Confusion
{
typedef T* (*(*type)[D1])[D2];
};

Confusion<T,3,4>::type *x = new Confusion<T,3,4>::type(42);

I think you meant:
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top