none
How to correctly declare variables (String, array) in Vicual C++ .NET (^ and spaces)?

    Question

  • How to correctly declare variables?
    In help it's written variously.

    1. String ^fileName1 = "abcde";
    2. String^ fileName1 = "abcde";
    3. String^fileName1 = "abcde";

    1. array<Byte> ^a1 = gcnew array<Byte>(32);
    2. array<Byte>^ a1 = gcnew array<Byte>(32);
    3. array<Byte>^a1 = gcnew array<Byte>(32);

    (All these three options are work without errors)

    I think that correct variant №1, because
    String^ a1, a2; - error
    String ^a1, a2; - error
    String ^a1, ^a2; - no error
    Wednesday, April 22, 2009 11:07 PM

Answers

  • They're all correct but represent different choices in style.

    Type^ variable emphasizes that the ^ is part of the type of the declared item and is probably the more common C++ coding style.

    Type ^variable emphasizes that the "base type" of the declared variable is "Type".  This style also more closely mirrors the way the grammar for C-like languages is defined.  This is common C coding style, where you'll see things like:

         int  a, *b, (*c)(int);

    delcaring an int variable a, a pointer to int b, a pointer to function c that takes an int parameter and returns an int.

    Many consider this kind of declaration to be a horrible abuse of an oscure corner of C delcarator syntax.  Others see it as an example of the brilliance of the designers of C to have come up with such a powerful declarator syntax.  Your mileage may vary.

    Type^variable is not a common coding style and more likely represents an inadvertant typo or other transformation that lost the whitespace between the identifiers and the operator character.

     


    -cd [VC++ MVP] Mark the best replies as answers!
    Thursday, April 23, 2009 12:39 AM
    Moderator

All replies

  • They're all correct but represent different choices in style.

    Type^ variable emphasizes that the ^ is part of the type of the declared item and is probably the more common C++ coding style.

    Type ^variable emphasizes that the "base type" of the declared variable is "Type".  This style also more closely mirrors the way the grammar for C-like languages is defined.  This is common C coding style, where you'll see things like:

         int  a, *b, (*c)(int);

    delcaring an int variable a, a pointer to int b, a pointer to function c that takes an int parameter and returns an int.

    Many consider this kind of declaration to be a horrible abuse of an oscure corner of C delcarator syntax.  Others see it as an example of the brilliance of the designers of C to have come up with such a powerful declarator syntax.  Your mileage may vary.

    Type^variable is not a common coding style and more likely represents an inadvertant typo or other transformation that lost the whitespace between the identifiers and the operator character.

     


    -cd [VC++ MVP] Mark the best replies as answers!
    Thursday, April 23, 2009 12:39 AM
    Moderator
  • It's not just style. It's a matter of understanding the correct syntax.  The carrot belongs on the variable name not the type name as can be evidenced by the use of a comma separated list:

    String^ a, b ; // a is ok but b isn't.
    String ^a, ^b ; // both are compilable.

    It's the same for C and C++.  The fact that they both ignore white space around the operators doesn't affect how it binds to the operand.  * and ^ bind to the variable name, not the type name.

    Joseph w Donahue joseph@odonahue.com www.odonahue.com
    Thursday, April 23, 2009 12:52 AM
  • Bjarne Stroustrup designed C++; you can read his explanation Is ``int* p;'' right or is ``int *p;'' right?.

     
    Sam Hobbs; see my SimpleSamples.Info
    Thursday, April 23, 2009 11:03 AM
  • I just found
    "C++ Programming Style Guidelines" (http://geosoft.no/development/cppstyle.html)

    ===
    51. C++ pointers and references should have their reference symbol next to the type rather than to the name. 
    float* x; // NOT: float *x;
    int& y;   // NOT: int &y;
     
    The pointer-ness or reference-ness of a variable is a property of the type rather than the name. C-programmers often use the alternative approach, while in C++ it has become more common to follow this recommendation. 
    ===

    Thursday, April 23, 2009 11:37 AM
  • I would consider Bjarne Stroustrup's comments to be more authoritative.
    Sam Hobbs; see my SimpleSamples.Info
    Thursday, April 23, 2009 1:00 PM