473,499 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instantiate (Constructor) syntax problem.

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

Jul 8 '06 #1
5 1082
On Sat, 8 Jul 2006 12:22:01 -0700, MilanB
<Mi****@discussions.microsoft.comwrote:
>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 Harrison
Visual C++ MVP
Jul 9 '06 #2
Thanks Doug

Your post is very usefull.

Thanks again
Milan

"Doug Harrison [MVP]" wrote:
On Sat, 8 Jul 2006 12:22:01 -0700, MilanB
<Mi****@discussions.microsoft.comwrote:
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 Harrison
Visual C++ MVP
Jul 9 '06 #3
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
Jul 9 '06 #4
On Sun, 9 Jul 2006 03:26:01 -0700, MilanB
<Mi****@discussions.microsoft.comwrote:
>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?

--
Doug Harrison
Visual C++ MVP
Jul 9 '06 #5
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 [MVP]" wrote:
On Sun, 9 Jul 2006 03:26:01 -0700, MilanB
<Mi****@discussions.microsoft.comwrote:
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?

--
Doug Harrison
Visual C++ MVP
Jul 10 '06 #6

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

Similar topics

2
2462
by: jerrygarciuh | last post by:
Hello, Is it possible to instantiate a child class within the constructor of its parent? eg Class DBI extends DB { function DBI() { // explicit parent constructor call
4
1673
by: GiBo | last post by:
Hi all, I have a class URI and a bunch of derived sub-classes for example HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is module urllib & friends, however my actual problem...
0
1229
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
3
3555
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.