473,566 Members | 2,776 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 1087
On Sat, 8 Jul 2006 12:22:01 -0700, MilanB
<Mi****@discuss ions.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****@discuss ions.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****@discuss ions.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****@discuss ions.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
2474
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
1676
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 however maps very well to this example). Now I want to pass a string to constructor of URI() and get an instance of one of the subclasses back....
0
1235
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 be instantiated. The user does not have
3
3562
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 be instantiated. The user does not have
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.