Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2006

Tip: Looking for answers? Try searching our database.

Instantiate (Constructor) syntax problem.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MilanB - 08 Jul 2006 20:22 GMT
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
Doug Harrison [MVP] - 09 Jul 2006 04:11 GMT
>Hello
>I new to intantiate following array:
[quoted text clipped - 5 lines]
>
>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. :)

Signature

Doug Harrison
Visual C++ MVP

MilanB - 09 Jul 2006 08:55 GMT
Thanks Doug

Your post is very usefull.

Thanks again
Milan

> >Hello
> >I new to intantiate following array:
[quoted text clipped - 29 lines]
> It may not make the end result any easier to understand, but at least it
> was easier to get there. :)
MilanB - 09 Jul 2006 11:26 GMT
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 Harrison [MVP] - 09 Jul 2006 23:34 GMT
>Doug,
>
[quoted text clipped - 3 lines]
>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?

Signature

Doug Harrison
Visual C++ MVP

MilanB - 10 Jul 2006 07:19 GMT
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,
> >
[quoted text clipped - 28 lines]
>
> In plain English, what type are you trying to create?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.