Udostępnij za pośrednictwem


null, nullptr, dull? Certainly not! New C++ 11 stuff

MH900160554nullptr is a new keyword in C++/11 which has been discussed all over the place, but I am writing a whole new way to look at the nullptr.  Not really.  Just kidding. Ok, I didn’t use the console write, I used Textboxes instead. So this old topic is revisited with a XAML interface.  Nothing new really.  But I had another idea of what to do with this blog entry but gave up on it and this is the ashes of my great idea.

There are five headings in this article.

ISO/IEC 14882 reference:

  • 2.14.7 Pointer literals
  • endnote 5 page 331

 

Why the nullptr instead of null?

From: https://msdn.microsoft.com/en-us/library/vstudio/jj651642(v=vs.120).aspx

Given func(std::pair<const char *, double>), then calling func(std::make_pair(NULL, 3.14)) causes a compiler error. The macro NULL expands to 0, so that the call std::make_pair(0, 3.14) returns std::pair<int, double>, which is not convertible to func()'s std::pair<const char *, double> parameter type. Calling func(std::make_pair(nullptr, 3.14)) successfully compiles because std::make_pair(nullptr, 3.14) returns std::pair<std::nullptr_t, double>, which is convertible to std::pair<const char *, double>.

Building the app:

To do this project, open Visual Studio New Project and select the C++ language and then select Windows Store, this will work with VS 2012 or VS 2013 running on Windows 8 or Windows 8.1.  You will need to make some modifications if you don’t use the filename when you create your app, I used:

MustMatchYourProjectName

For my project name.  Yep, I really did use “MustMatchYourProjectName”, which means you have to make some modifications.

XAML

The XAML code uses a Viewbox, which does the rotation and snap automatically!  The Grid is the one provided with the template.  The Stackpanel allows you to have a neat look to your project.  The Textboxes show your output.  The button implements the code.  Gee just like Visual Basic or C#!  nullprt is equal to nothing in Visual Basic and null in C#

<!-------------------- XAML Code,  replace the <Grid ..></Grid>--------------------------------------------—>

<Viewbox>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            < StackPanel Orientation="Horizontal" Margin="20" VerticalAlignment="Center">
                < TextBox Width="150" Margin="10" x:Name="nullptr1" FontSize="11" Text="0" TextAlignment="Right"/>               
                < TextBox Width="150" Margin="10" x:Name="nullptr2" FontSize="11" Text="0" TextAlignment="Right"/>
                < TextBox Width="150" Margin="10" x:Name="nullptr3" FontSize="11" Text="0" TextAlignment="Right"/>
                < TextBox Width="150" Margin="10" x:Name="nullptr4" FontSize="11" Text="0" TextAlignment="Right"/>
                <Button Content="Do the nullptr" Margin="10" FontSize="25" Tapped="NullPtrDemo" />
             </StackPanel>
        </Grid>
    </Viewbox>

<!------------------ End of XAML Code --------------------------------------------------------------------------->

C++ Code

//***************Copy the following to test the nullptr *************************************************

//Start of the source file or CPP file, just copy the code and paste into the MainPage.xaml.cpp.

#include "pch.h"
#include "MainPage.xaml.h"

using namespace MustMatchYourProjectName;;
using namespace Platform;

MainPage::MainPage()
{
    InitializeComponent();
}

class MyClass {
public:
    int i;
};

//From https://msdn.microsoft.com/en-us/library/4ex65770(v=vs.120).aspx
//With Changes made to use the XAML interface
void MustMatchYourProjectName::MainPage::NullPtrDemo(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{   
        MyClass *pMyClass = nullptr;
        if (pMyClass == nullptr)
        {
            nullptr1->Text="pMyClass == nullptr";
        }           

        if (pMyClass == 0)
        {
            nullptr2->Text = "pMyClass is now set equal";
        }

        pMyClass = 0;
        if (pMyClass == nullptr)
        {
            nullptr3->Text = "pMyClass == nullptr";
        }

        if (pMyClass == 0)
        {
             nullptr4->Text = "pMyClass == 0";
         }
}

//********** End of Copy and paste for MainPage.xaml.cpp **********************