<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aastha Gupta</title>
    <description>The latest articles on DEV Community by Aastha Gupta (@guptaaastha).</description>
    <link>https://dev.to/guptaaastha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F617076%2F6ab878a5-706e-41e0-b49c-cd3ac50e1e75.jpeg</url>
      <title>DEV Community: Aastha Gupta</title>
      <link>https://dev.to/guptaaastha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guptaaastha"/>
    <language>en</language>
    <item>
      <title>Quick Introduction to class in C++</title>
      <dc:creator>Aastha Gupta</dc:creator>
      <pubDate>Wed, 07 Jul 2021 13:32:59 +0000</pubDate>
      <link>https://dev.to/guptaaastha/quick-introduction-to-class-in-c-57je</link>
      <guid>https://dev.to/guptaaastha/quick-introduction-to-class-in-c-57je</guid>
      <description>&lt;p&gt;A class is a data type that you define and you can also add some functions that work on this data type. That's it, that is exactly what a class is! Thank you for reading this article and I'll see you in the next one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;wait&lt;/strong&gt; . . .&lt;/p&gt;

&lt;p&gt;Above explanation is simple and covers what a class is in as little words as possible but there are more caveats related to it and I'll go over them in this article. &lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring a class
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;private:&lt;/span&gt;

      &lt;span class="c1"&gt;// data memebers&lt;/span&gt;
      &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;legs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="nl"&gt;public:&lt;/span&gt;
      &lt;span class="c1"&gt;// constructor&lt;/span&gt;
      &lt;span class="n"&gt;Cat&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;breed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Sphynx"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;legs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;meow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// member function&lt;/span&gt;
      &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;setLegs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;legs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// member function&lt;/span&gt;
      &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;setBreed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;breed_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;breed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;breed_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Data members
&lt;/h4&gt;

&lt;p&gt;What you create of classes are &lt;code&gt;objects&lt;/code&gt;. These &lt;code&gt;objects&lt;/code&gt; have certain properties that can be accessed and these can be defined in the class as &lt;strong&gt;data members&lt;/strong&gt;. For a Table it can be wood type, number of drawers, length, height etc. By creating an object of a table, you can assign values to these properties in accordance with their data types.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A class is a plan, object it's execution &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Member functions
&lt;/h4&gt;

&lt;p&gt;You might want your objects to perform some tasks and for that you write member functions. These functions are like regular functions accept they are only accessible to the object of the class; which means the functions can or can not take parameters, it can or cannot or return something back. Member functions are a powerful and you must use them with caution.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Inheritance&lt;/em&gt; in C++ can be achieved by deriving a class from another class of which access specifiers are an important part. I'll pick these topics up in my future articles. Stay tuned! If you want to read more about classes, head over &lt;a href="https://en.cppreference.com/w/cpp/language/class"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for giving this article a read and I'll see you in the next one 😄&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: This is an article in my series Quick Introduction to a concept in C++. You can find all the articles in this series &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-a-concept-in-c-15n6"&gt;here&lt;/a&gt;. I also answer why I don't use the series feature by &lt;a href="https://dev.to/guptaaastha/"&gt;dev.to&lt;/a&gt; there.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cpp</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>class</category>
    </item>
    <item>
      <title>Quick Introduction to header files in C++</title>
      <dc:creator>Aastha Gupta</dc:creator>
      <pubDate>Mon, 28 Jun 2021 16:19:09 +0000</pubDate>
      <link>https://dev.to/guptaaastha/quick-introduction-to-header-files-in-c-4fda</link>
      <guid>https://dev.to/guptaaastha/quick-introduction-to-header-files-in-c-4fda</guid>
      <description>&lt;p&gt;Using a variable (or function or a class and so on) without declaring it, is an error in C++; more specifically, this error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In function 'int main()':
error: 'x' was not declared in this scope
  x = 2;
  ^
[ERROR in Compilation]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which means, it is important to declare an element before using it or assigning it a value, in C++.&lt;/p&gt;

&lt;p&gt;(The word &lt;code&gt;element&lt;/code&gt; refers to functions, classes, structs, variables etc throughout this article.)&lt;/p&gt;

&lt;p&gt;We might end up declaring and defining various &lt;code&gt;elements&lt;/code&gt; repeatedly in our different &lt;code&gt;C++&lt;/code&gt; programs if they're useful in varied cases. This would result in a lot of code repetition throughout our code-base; and making one change in an &lt;code&gt;element&lt;/code&gt; would result in copying the same change over and over again in different files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sounds scary !&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Therefore, it makes sense to declare and define commonly used &lt;code&gt;elements&lt;/code&gt; that are predicted to be of use often before hand and use them later without worrying about their declaration. To keep our code modular, it fits to combine such 'utility' &lt;code&gt;elements&lt;/code&gt; in one single file and keep importing it in other &lt;code&gt;c++&lt;/code&gt; programs. &lt;/p&gt;

&lt;p&gt;Enter &lt;code&gt;header files&lt;/code&gt;; these are declarations and definitions of various &lt;code&gt;elements&lt;/code&gt; which are ready to be used in a &lt;code&gt;c++&lt;/code&gt; program. Header files have a &lt;code&gt;.h&lt;/code&gt; extension instead of a &lt;code&gt;.cpp&lt;/code&gt; one; this tells everyone that &lt;code&gt;my_file.h&lt;/code&gt; is a header file that contains declarations and definitions of various 'utility' &lt;code&gt;elements&lt;/code&gt;. A header file can than be included in a &lt;code&gt;cpp&lt;/code&gt; program as such :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fss93ebotx4tec7kfkcvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fss93ebotx4tec7kfkcvx.png" alt="include_header_file_example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Want to write good a header file? Keep the following points in mind while writing one :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Do not bring complete &lt;code&gt;namespaces&lt;/code&gt; into scope with the &lt;code&gt;using&lt;/code&gt; directive (read more about &lt;code&gt;using&lt;/code&gt; &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-using-in-c-4n73"&gt;here&lt;/a&gt;) as this might conflict with other &lt;code&gt;elements&lt;/code&gt; present in the file where this header is being &lt;code&gt;included&lt;/code&gt; into.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;const&lt;/code&gt; variable definitions to make sure a program &lt;code&gt;including&lt;/code&gt; this header file cannot change it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;inline&lt;/code&gt; function definitions and named &lt;code&gt;namespaces&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Standard library header files enable various useful functionalities in our C++ programs. Moreover, we can write our own header files to make our code-base modular and thus, more understandable.&lt;/p&gt;

&lt;p&gt;This article was a short introduction and if you want to know more about various header files in C++, I encourage you to head over &lt;a href="https://en.cppreference.com/w/cpp/header" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for giving this article a read and I'll see you in the next one 😄&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: This is an article in my series Quick Introduction to a concept in C++. You can find all the articles in this series &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-a-concept-in-c-15n6"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>headerfiles</category>
    </item>
    <item>
      <title>Quick Introduction to *a concept* in C++</title>
      <dc:creator>Aastha Gupta</dc:creator>
      <pubDate>Thu, 17 Jun 2021 15:48:06 +0000</pubDate>
      <link>https://dev.to/guptaaastha/quick-introduction-to-a-concept-in-c-15n6</link>
      <guid>https://dev.to/guptaaastha/quick-introduction-to-a-concept-in-c-15n6</guid>
      <description>&lt;p&gt;I've been writing C++ programs since past 5 years now (I didn't realize it has been that long until I typed this out) and have come across various concepts that beginners seem to struggle with. &lt;/p&gt;

&lt;p&gt;To help my fellow programmers in the community, I started a series of articles called &lt;strong&gt;Quick Introduction to &lt;em&gt;a concept&lt;/em&gt; in C++&lt;/strong&gt;. These articles are generally 2 to 5 minute reads that cover a &lt;code&gt;specific concept&lt;/code&gt; in C++ in simple words, contain examples and point to comprehensive resources as well.&lt;/p&gt;

&lt;p&gt;I read and reply to comments often, and you can suggest any topics you'd like to be covered in this series.&lt;/p&gt;

&lt;p&gt;Here is a list of all the articles currently in the series :&lt;br&gt;
(I'll keep updating this list as and when I write new articles)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/guptaaastha/quick-introduction-to-namespaces-in-c-2pmg"&gt;Quick Introduction to namespaces in C++&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/guptaaastha/quick-introduction-to-using-in-c-4n73"&gt;Quick Introduction to using in C++&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/guptaaastha/quick-introduction-to-typedef-in-c-5362"&gt;Quick Introduction to typedef in C++&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/guptaaastha/quick-introduction-to-header-files-in-c-4fda"&gt;Quick Introduction to header files in C++&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/guptaaastha/quick-introduction-to-class-in-c-57je"&gt;Quick Introduction to class in C++&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why don't you use the &lt;strong&gt;series&lt;/strong&gt; feature provide by dev.to?&lt;br&gt;
Simply because it breaks the flow of the article. Different articles in the series are crowded on the top of the current article, which diverts the attention of the reader. I personally do not like that and therefore take the pain to update this post every time I write a new article in the series &lt;strong&gt;Quick Introduction to &lt;em&gt;a concept&lt;/em&gt; in C++&lt;/strong&gt; :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider reacting to the articles with a like, unicorn or a comment and sharing it with your friends if you like them and appreciate my efforts put into writing them. This helps the article reach more people who might benefit from these; no pressure though, I appreciate you just reading my article irrespective of you sharing or reacting to it.&lt;/p&gt;

&lt;p&gt;As always, thanks for giving this article a read and I'll see you in the next one 😄&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Quick Introduction to namespaces in C++</title>
      <dc:creator>Aastha Gupta</dc:creator>
      <pubDate>Thu, 17 Jun 2021 15:43:43 +0000</pubDate>
      <link>https://dev.to/guptaaastha/quick-introduction-to-namespaces-in-c-2pmg</link>
      <guid>https://dev.to/guptaaastha/quick-introduction-to-namespaces-in-c-2pmg</guid>
      <description>&lt;p&gt;As someone who writes C++ code often, I see (and use) the &lt;code&gt;keyword&lt;/code&gt; &lt;code&gt;namespace&lt;/code&gt; frequently and I think it is worth knowing what this &lt;code&gt;keyword&lt;/code&gt; is all about.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;namespace&lt;/code&gt; is a region that provides a &lt;code&gt;scope&lt;/code&gt; to the &lt;code&gt;identifiers&lt;/code&gt; (the names of functions, variables, etc) inside it. &lt;code&gt;namespaces&lt;/code&gt; are used to organize code into &lt;code&gt;logical groups&lt;/code&gt;. All &lt;code&gt;identifiers&lt;/code&gt; in the &lt;code&gt;namespace&lt;/code&gt; scope are visible to one another. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;identifiers&lt;/code&gt; in the &lt;code&gt;namespace&lt;/code&gt; can be accessed by bringing those said &lt;code&gt;identifiers&lt;/code&gt; into the current scope by &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-using-in-c-4n73"&gt;&lt;code&gt;using&lt;/code&gt;&lt;/a&gt; or utilizing fully qualified names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// the namespace std is now in scope&lt;/span&gt;
&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;
&lt;span class="c1"&gt;// fully qualified name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Declaring &lt;code&gt;namespace&lt;/code&gt; and &lt;code&gt;namespace members&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;namespace&lt;/code&gt; declaration is generally done in a header file as following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// my_namespace_declaration.h&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;my_own_namepsace&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;func_one&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;func_two&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The members of this &lt;code&gt;namespace&lt;/code&gt; can then be brought into &lt;code&gt;scope&lt;/code&gt; of any other file by fully qualified names or the &lt;code&gt;using&lt;/code&gt; directive. (If you want to know more about &lt;code&gt;using&lt;/code&gt;, checkout &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-using-in-c-4n73"&gt;this&lt;/a&gt; article by me)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// other_file.cpp&lt;/span&gt;

&lt;span class="cp"&gt;#include"my_own_namepsace.h"
&lt;/span&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;my_own_namepsace&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;my_own_namepsace&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;func_one&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// fully qualified names are to be used&lt;/span&gt;
&lt;span class="c1"&gt;// even when bringing the namespace in &lt;/span&gt;
&lt;span class="c1"&gt;// scope with using directive&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code in a header file that uses &lt;code&gt;identifiers&lt;/code&gt; from other &lt;code&gt;namespaces&lt;/code&gt; should always use the fully qualified &lt;code&gt;namespace&lt;/code&gt; name to avoid ambiguity. &lt;a href="http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-using-directive"&gt;This&lt;/a&gt; isocpp core guideline puts more light.   &lt;/p&gt;

&lt;h4&gt;
  
  
  Nested namespaces
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;namespaces&lt;/code&gt; can be nested. A nested namespace has unqualified access to its parent's members however the parent members do not have unqualified access to the nested namespace (unless it is declared as &lt;a href="https://en.cppreference.com/w/cpp/language/namespace#:~:text=An%20inline%20namespace%20is%20a,many%20situations%20(listed%20below)."&gt;inline&lt;/a&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  The std namespace
&lt;/h4&gt;

&lt;p&gt;All C++ standard library types and functions are declared in the &lt;code&gt;std namespace&lt;/code&gt; or the various namespaces that are nested inside &lt;code&gt;std&lt;/code&gt; which makes it one of the most common &lt;code&gt;namespaces&lt;/code&gt; to be encountered.&lt;/p&gt;

&lt;p&gt;This article is a short introduction and if you want to know more about &lt;code&gt;namespaces&lt;/code&gt;, I encourage you to head over &lt;a href="https://en.cppreference.com/w/cpp/language/namespace"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for giving this article a read and I'll see you in the next one 😄&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: This is an article in my series &lt;strong&gt;Quick Introduction to &lt;em&gt;a concept&lt;/em&gt; in C++&lt;/strong&gt;. You can find all the articles in this series &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-a-concept-in-c-15n6"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cpp</category>
      <category>namespace</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>typedef VS using in type aliases declaration in C++</title>
      <dc:creator>Aastha Gupta</dc:creator>
      <pubDate>Mon, 14 Jun 2021 07:47:24 +0000</pubDate>
      <link>https://dev.to/guptaaastha/typedef-vs-using-in-type-aliases-declaration-in-c-fm2</link>
      <guid>https://dev.to/guptaaastha/typedef-vs-using-in-type-aliases-declaration-in-c-fm2</guid>
      <description>&lt;p&gt;In my previous posts, &lt;code&gt;typedef&lt;/code&gt; and &lt;code&gt;using&lt;/code&gt; were introduced. If you haven't read them, I'd recommend to check them out first before proceeding with this one ( &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-typedef-in-c-5362"&gt;&lt;code&gt;typedef&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-using-in-c-4n73"&gt;&lt;code&gt;using&lt;/code&gt;&lt;/a&gt; ).&lt;/p&gt;

&lt;p&gt;Let's look at the following code snippets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios_base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fmtflags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios_base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fmtflags&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both these code snippets are equivalent! Infact, In C++11, the &lt;code&gt;using&lt;/code&gt; keyword when used for type alias is identical to &lt;code&gt;typedef&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;using&lt;/code&gt; is used specifically to type-alias &lt;code&gt;template&lt;/code&gt; instead of &lt;code&gt;typedef&lt;/code&gt; and &lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1489.pdf"&gt;this paper&lt;/a&gt; explains it very well so.&lt;/p&gt;

&lt;p&gt;An excerpt from the paper:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It has been suggested to (re)use the keyword &lt;code&gt;typedef&lt;/code&gt; — as done in the paper [4] — to introduce template aliases:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
   &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MyAllocator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;That notation has the advantage of using a keyword already known to introduce a type alias. However, it also displays several disadvantages among which the confusion of using a keyword known to introduce an alias for a type-name in a context where the alias does not designate a type, but a template; &lt;code&gt;Vec&lt;/code&gt; is not an alias for a type, and should not be taken for a &lt;code&gt;typedef-name&lt;/code&gt;. &lt;em&gt;The name &lt;code&gt;Vec&lt;/code&gt; is a name for the family&lt;/em&gt; &lt;code&gt;std::vector&amp;lt; [bullet] , MyAllocator&amp;lt; [bullet] &amp;gt; &amp;gt;&lt;/code&gt; – where the bullet is a placeholder for a type-name. Consequently we do not propose the &lt;code&gt;typedef&lt;/code&gt; syntax. On the other hand the sentence&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
    &lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MyAllocator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;can be read/interpreted as: from now on, &lt;em&gt;I’ll be using &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt; as a synonym for &lt;code&gt;std::vector&amp;lt;T, MyAllocator&amp;lt;T&amp;gt; &amp;gt;&lt;/code&gt;&lt;/em&gt;. With that reading, the new syntax for aliasing seems reasonably logical.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might wonder why a new keyword was not introduced and why &lt;code&gt;using&lt;/code&gt; was reused; there is infact a pretty good reason behind the rationale of not introducing a new keyword or new syntax every so often. The standard wants to avoid breaking old code as much as possible. This is why in proposal documents you might encounter sections like &lt;strong&gt;&lt;em&gt;Impact on the Standard&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;Design decisions&lt;/em&gt;&lt;/strong&gt;, and how they might affect older code. There are situations when a proposal seems like a really good idea but might not have traction because it would be too difficult to implement or too confusing, or would contradict old code which is something that should be avoided.&lt;/p&gt;

&lt;p&gt;Thanks for giving this article a read and I'll see you in the next one 😄&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quick Introduction to using in C++</title>
      <dc:creator>Aastha Gupta</dc:creator>
      <pubDate>Mon, 14 Jun 2021 02:47:47 +0000</pubDate>
      <link>https://dev.to/guptaaastha/quick-introduction-to-using-in-c-4n73</link>
      <guid>https://dev.to/guptaaastha/quick-introduction-to-using-in-c-4n73</guid>
      <description>&lt;p&gt;If you've ever written a c++ code snippet, I predict that you've seen this particular line :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and thought to yourself, 'what does &lt;code&gt;using&lt;/code&gt; mean?'&lt;/p&gt;

&lt;p&gt;&lt;code&gt;using&lt;/code&gt; is a &lt;code&gt;keyword&lt;/code&gt; in C++ which is applicable in the following cases:&lt;/p&gt;

&lt;h4&gt;
  
  
  Bring all members from the namespace into​ the current scope.
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;using&lt;/code&gt; directive allows all the names in a namespace to be used without the namespace-name as an explicit qualifier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// brings all namespace names in the current scope&lt;/span&gt;
&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// std::string can now be referred as string throughout the scope&lt;/span&gt;

&lt;span class="n"&gt;OR&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// string is now recognized, &lt;/span&gt;
&lt;span class="c1"&gt;// however, will have to be addressed as std::string &lt;/span&gt;
&lt;span class="c1"&gt;// wherever required&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now you know what &lt;code&gt;using&lt;/code&gt; does in :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It brings &lt;code&gt;namespace&lt;/code&gt; std in scope which makes it handy to use names in std without the explicit prefix &lt;code&gt;std::&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If a local variable has the same name as a &lt;code&gt;namespace&lt;/code&gt; variable, the &lt;code&gt;namespace&lt;/code&gt; variable is hidden. It is, therefore, a good practice to not bring &lt;em&gt;complete namespaces&lt;/em&gt; in scope in long code files to avoid such cases where an intuitive identifier name has to be dropped 🙁 because same name exists in the &lt;code&gt;namespace&lt;/code&gt; brought in scope by &lt;code&gt;using&lt;/code&gt;. A workaround is to bring a specific name from a namespace in scope as:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Bring a base class method ​or variable into the current class’ scope.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BaseClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;public:&lt;/span&gt;
      &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Hi there, I am Base"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DerivedClass&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BaseClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;public:&lt;/span&gt;
      &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Hi, "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="c1"&gt;// Instead of recursing, the greet() method&lt;/span&gt;
         &lt;span class="c1"&gt;// of the base class is called.&lt;/span&gt;
         &lt;span class="n"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create type aliases
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;MyName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyVector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MyAlloc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="n"&gt;MyName&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// sample usage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ios_base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fmtflags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// identical to &lt;/span&gt;
&lt;span class="c1"&gt;// typedef std::ios_base::fmtflags flags;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;typedef&lt;/code&gt; already exists in the C++ language which makes one wonder, why &lt;code&gt;using&lt;/code&gt; was introduced to create type aliases. This is a valid question and an interesting one. There are a few interesting facts associated with &lt;code&gt;typedef&lt;/code&gt; and &lt;code&gt;using&lt;/code&gt; type aliases which you can read in &lt;a href="https://dev.to/guptaaastha/typedef-vs-using-in-type-aliases-declaration-in-c-fm2"&gt;this post&lt;/a&gt; 😄.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are more caveats to &lt;code&gt;using&lt;/code&gt; but are not covered here because this is a quick introduction, and also because of how rarely they are encountered. I encourage you to read more about &lt;code&gt;using&lt;/code&gt; &lt;a href="https://en.cppreference.com/w/cpp/keyword/using"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for giving this article a read and I'll see you in the next one 😄&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: This is an article in my series Quick Introduction to &lt;em&gt;a concept&lt;/em&gt; in C++. You can find all the articles in this series &lt;a href="https://dev.to/guptaaastha/quick-introduction-to-a-concept-in-c-15n6"&gt;here&lt;/a&gt;. I also answer why I don't use the series feature by &lt;a href="https://dev.to/guptaaastha/"&gt;dev.to&lt;/a&gt; there.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cpp</category>
      <category>using</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quick Introduction to typedef in C++</title>
      <dc:creator>Aastha Gupta</dc:creator>
      <pubDate>Sat, 08 May 2021 06:45:25 +0000</pubDate>
      <link>https://dev.to/guptaaastha/quick-introduction-to-typedef-in-c-5362</link>
      <guid>https://dev.to/guptaaastha/quick-introduction-to-typedef-in-c-5362</guid>
      <description>&lt;p&gt;C++ is a strongly typed language which means we take types very seriously in the C++ community! While types are important and reduce bugs, it gets tiresome pretty quickly when there's an inbuilt type with a long name that you’ve to use repetitively throughout your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Examples&lt;/span&gt;

&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;product_one&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Introducing &lt;code&gt;typedef&lt;/code&gt; declarations, easy effortless way to declare and use types in your file. &lt;code&gt;typedef&lt;/code&gt; declarations can be considered as nicknames✨ that you give to an inbuilt type and whenever you want to say &lt;code&gt;inbuilt-type&lt;/code&gt;, you say their &lt;em&gt;nickname&lt;/em&gt; instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;UL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="n"&gt;UL&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Equivalent to unsigned long num;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;PRODUCT&lt;/span&gt;
&lt;span class="n"&gt;PRODUCT&lt;/span&gt; &lt;span class="nf"&gt;product_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"notebook"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;54.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Equivalent to std::pair &amp;lt;std::string,double&amp;gt; ("notebook",54.5);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In contrast to the &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;union&lt;/code&gt;, and &lt;code&gt;enum&lt;/code&gt; declarations, &lt;code&gt;typedef declarations&lt;/code&gt; &lt;em&gt;do not introduce new types&lt;/em&gt;, they simply introduce new names for existing types. What’s more exciting is that you can declare any type with &lt;code&gt;typedef&lt;/code&gt;, including pointers, functions and array types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;}&lt;/span&gt; &lt;span class="n"&gt;STRUCT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p_STRUCT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// the following two objects have the same type&lt;/span&gt;
&lt;span class="n"&gt;pSTRUCT&lt;/span&gt; &lt;span class="n"&gt;p_struct_1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;STRUCT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p_struct_2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;typedef&lt;/code&gt; declarations are &lt;em&gt;scoped&lt;/em&gt;, that is you can declare a &lt;code&gt;variable&lt;/code&gt; with the same name as of the &lt;code&gt;typedef&lt;/code&gt; in the same file in a different scope and you’ll face no type errors whatsoever!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;UL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;UL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   
   &lt;span class="c1"&gt;// re-declaration hides typedef name&lt;/span&gt;
   &lt;span class="c1"&gt;// now UL is an unsigned int variable in this scope&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// typedef UL back in scope&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, I would highly advise against this practice because it eventually makes your file less readable and more confusing which is not something you’d want to do.🤷&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In summary, you can use &lt;code&gt;typedef&lt;/code&gt; declarations to construct shorter and more meaningful names to the types that are already defined by the language or for the types that you have declared. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hopefully this article gave you a brief introduction to typedef and you had fun reading it. To get more information on the topic, refer the docs &lt;a href="https://en.cppreference.com/w/cpp/language/typedef"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Thanks for giving this article a read and I'll see you in the next one 😄  &lt;/p&gt;

</description>
      <category>cpp</category>
      <category>typedef</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
