Announcement

Collapse
No announcement yet.

C++ Syntax Guide?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    C++ Syntax Guide?

    I am good in C++ concepts but I'm having hard time in understanding C++ syntax.
    Like in this example why ":elem" is written before curly braces.

    PHP Code:
    class Vector { public:
    Vector(int s) :elem{new double[s]}, sz{s} { } 
    And this is a different for loop syntax.

    PHP Code:
    for (autov) ++x
    Can you guide me where can I learn this type of advanced syntax?

    #2
    Originally posted by Inder View Post
    Code:
    class Vector 
    { 
    public:
         Vector(int s) : elem{new double[s]}, sz{s} { }
    The stuff between the colon and the curly braces is called the member initializer list.

    Originally posted by Inder View Post
    Code:
    for (auto& x : v) ++x;
    That's a range based for loop.
    Contact: enlight in #unrealengine IRC channel on Freenode, or @macagonator on Twitter

    Comment


      #3
      Not assuming your level here, but a very interesting Wiki entry which helped a lot can be found here https://wiki.unrealengine.com/Entry_...to_UE4_C%2B%2B
      - Martin

      A Simple Prototype

      https://www.martinegger.org/

      Technical Producer

      Comment


        #4
        I'd suggest reading a C++11 book if you want to learn those concepts, the for loop right below is what's called a range-based for loop; it makes use of iterators provided by your container class. A book I'd recommend is the C++ programming language that focuses on C++11, first a few examples. Let's say you create an std::vector to store the first 9 natural numbers, let's say you want to loop through this vector and see what it contains by printing out each element, but instead you want to make use of the new range-based for loop. You'd do it like so, the auto keyword is a new feature - basically auto deduction during compile time so you don't have to figure out what i is, very useful for lambdas and such.
        Code:
        int main()
        {
            std::vector<int> NaturalNumbers{1,2,3,4,5,6,7,8,9};
        
            for (auto i : NaturalNumbers){
                std::cout << i << std::endl;
            }
        }
        Now note that I didn't use an ampersand because I only needed a copy of the elements, on the other hand if you need a reference then you'll want to use an ampersand.

        To see the difference, try this out.

        Code:
        int main()
        {
            std::vector<int> NaturalNumbers{1,2,3,4,5,6,7,8,9};
        
            for (auto i : NaturalNumbers){
                i = 1;
                std::cout << i << std::endl;
            }
            std::cout << std::endl;
            for (auto i : NaturalNumbers){
                std::cout << i << std::endl;
            }
        }
        Code:
        int main()
        {
            std::vector<int> NaturalNumbers{1,2,3,4,5,6,7,8,9};
        
            for (auto &i : NaturalNumbers){
                i = 1;
                std::cout << i << std::endl;
            }
            std::cout << std::endl;
            for (auto i : NaturalNumbers){
                std::cout << i << std::endl;
            }
        }
        In the last version, the elements in the container gets modified as it is accessed by reference and not by value. Just remember that to make use of this feature, your class should implement the begin() and end() member functions.

        As for the last question, this is essentially constructing and assignment values to members of the class, I'd advice using an initialization list than assignment in the constructor of your class for performance.

        Comment


          #5
          Nearly all sugar of C++ is explained here: http://www.cplusplus.com/
          Sr. Engine Programmer @ www.playspace.com - moritzwundke.com
          Remember: be polite and respect other peoples opinions - Join the Unofficial Unreal Discord Channel - Found a bug? Then use the Bug Report Form to get it fixed ^^

          Comment

          Working...
          X