Instantiate (Constructor) syntax problem.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello
I new to intantiate following array:
CString (**newArray)[2]

I tried following syntax, by fail.
int count = 10;
CString (**newArray)[2] = new (CString*[2])[count];

How to write correct syntax?

Thanks
Milan
 
Hello
I new to intantiate following array:
CString (**newArray)[2]

I tried following syntax, by fail.
int count = 10;
CString (**newArray)[2] = new (CString*[2])[count];

How to write correct syntax?

On the LHS, you have declared a pointer to a pointer to CString[2]. This
means the RHS should create an object of type CString (*)[2] or an array of
such objects:

// Scalar
CString (**p)[2] = new (CString (*)[2]);
// Array
CString (**q)[2] = new (CString (*[count])[2]);

For these complex mixtures of pointer and array types, typedef can be your
best friend, and you can define a series of typedefs that culminate in the
type you're really after. For example, this is equivalent to the above but
a lot simpler to deal with:

typedef CString CString2[2];
typedef CString2* CString2P;
// Scalar
CString2P* p = new CString2P;
// Array
CString2P* q = new CString2P[count];

It may not make the end result any easier to understand, but at least it
was easier to get there. :)
 
Thanks Doug

Your post is very usefull.

Thanks again
Milan

Doug Harrison said:
Hello
I new to intantiate following array:
CString (**newArray)[2]

I tried following syntax, by fail.
int count = 10;
CString (**newArray)[2] = new (CString*[2])[count];

How to write correct syntax?

On the LHS, you have declared a pointer to a pointer to CString[2]. This
means the RHS should create an object of type CString (*)[2] or an array of
such objects:

// Scalar
CString (**p)[2] = new (CString (*)[2]);
// Array
CString (**q)[2] = new (CString (*[count])[2]);

For these complex mixtures of pointer and array types, typedef can be your
best friend, and you can define a series of typedefs that culminate in the
type you're really after. For example, this is equivalent to the above but
a lot simpler to deal with:

typedef CString CString2[2];
typedef CString2* CString2P;
// Scalar
CString2P* p = new CString2P;
// Array
CString2P* q = new CString2P[count];

It may not make the end result any easier to understand, but at least it
was easier to get there. :)
 
Doug,

If I have have declared a pointer to CString[2]. How to create object of
CString[2] (syntax):

For example:
CString (*llv)[2] = new CString[2]; //Here fail right side

Thanks a lot
Milan
 
Doug,

If I have have declared a pointer to CString[2]. How to create object of
CString[2] (syntax):

For example:
CString (*llv)[2] = new CString[2]; //Here fail right side

The expression:

new CString[2]

creates an array of two CStrings and returns a pointer to its first
element; this pointer has type CString*, as is the case for any array
CString[n]. So if your RHS is correct, you need:

CString* llv = new CString[2]; // Fine

On the other hand, if your LHS is correct, you're talking about a pointer
to an array of two CStrings, which is different, because the thing pointed
to is a CString[2], not a CString. Pointers to arrays come into play when
dealing with 2D arrays. For example:

CString (*llv)[2] = new CString[2][2]; // Fine
CString (*llv)[2] = new CString[3][2]; // Fine
CString (*llv)[2] = new CString[4][2]; // Fine

Here, new[] returns a pointer to the first element of an array
CString[m][2]. This element has the type CString[2], and so a pointer to it
has the type CString (*)[2].

In plain English, what type are you trying to create?
 
Thanks Doug,

I have already solved my problem using your previous answer. I just was
experimenting, and was not sure about my second question ( could not found
answer on web and documentation, so I asked you)

Thanks again for your very, very usefull and precise answers.
Milan

Doug Harrison said:
Doug,

If I have have declared a pointer to CString[2]. How to create object of
CString[2] (syntax):

For example:
CString (*llv)[2] = new CString[2]; //Here fail right side

The expression:

new CString[2]

creates an array of two CStrings and returns a pointer to its first
element; this pointer has type CString*, as is the case for any array
CString[n]. So if your RHS is correct, you need:

CString* llv = new CString[2]; // Fine

On the other hand, if your LHS is correct, you're talking about a pointer
to an array of two CStrings, which is different, because the thing pointed
to is a CString[2], not a CString. Pointers to arrays come into play when
dealing with 2D arrays. For example:

CString (*llv)[2] = new CString[2][2]; // Fine
CString (*llv)[2] = new CString[3][2]; // Fine
CString (*llv)[2] = new CString[4][2]; // Fine

Here, new[] returns a pointer to the first element of an array
CString[m][2]. This element has the type CString[2], and so a pointer to it
has the type CString (*)[2].

In plain English, what type are you trying to create?
 
Back
Top