C++/CX: Inheritance in the CX, set up the project

Inheritance is a good thing if an uncle that you never met dies and leaves you a few million dollars, that is a good thing since you didn’t know him, and it is nice to have a few extra million dollars. 

Why did you do this Sam?

Seriously, C++ suffers from the console apps, but it also survived because of the ability to experiment to with new ideas.  I didn’t succeed, but it gets you there quickly.  I did try to start from a console app, but could not figure out the settings and frankly just wanted to try this.  So that is why.  And maybe one of my friends from India and pull my posterior out of the fire on this one. 

Now the discussion (Make sure to scroll down to see how to implement the project)

But inheritance in software does change and in C++/CX you might want to create a simple app that you can use to test it without all of the other useful stuff like XAML.  So how do you build a console app that will run at least enough to step through the programming, and feel like you get your arms around the code.

Warning: You have to read this before moving ahead (and then scroll down)

Note: This code will only run if you are debugging using breakpoint. The project will add an icon to the Start Screen but it will throw an error if you press F5 to run, and the error is somewhat confusing, I even tried to chase down the reason and I WROTE the FREAKING project KNOWING it isn’t an actual Windows Store app.  So you have been warned, you have to use at least one Breakpoint and step through the project.  Here is the error that wasted about an hour until I remembered what I was doing:

image

So how do you set this up?

Here is how I got the code to the state that I have one source file (cpp extension), no header file (.h)

  1. Start New Project in VS 2012
  2. Select Windows Store Direct3D App and give it a useful name that you remember
  3. The project solution folder looks like this initially:image
  4. Delete all of the files except for:
    • Assets folder
    • Common folder
    • External Dependencies
    • BasicTimer.h
    • Package.appxmanifest
    • pch.cpp
    • pch.h
    • YourFileName.cpp, which in this case is the file Direct3DApp1.cpp

Your Solution folder might look similar to this (if you accidentally delete BasicTimer.h, that is ok for this blog)

 

The Solution Explorer should looks like:

image

Modify the source code

Make Modifications to the single source file so you can clearly see the step, and do other experiments with classes, etc.  But keep in mind it will run only if you have at least one breakpoint.  And darn it I just ran it again without the breakpoint!  So beware, it will build with no error, but run it without the breakpoint, and you get that error!

Ok, what is the code, will discuss it in my next blog, add the following code into your (hopefully) renamed Direct3DApp1.cpp, the header data is included in the code:

 /********* Beginning of Code to copy ********/
#include "pch.h"
//#include "Direct3DApp1.h"
#include "BasicTimer.h"
 
 
 
 
 
// Name        : Direct3DApp1.cpp
// Description : A C++/CX inheritance sample
 
interface struct IPickGet
{
    int GetPick() = 0;
};
 
interface struct IPickSet
{
 void SetPick(int value) = 0;
};
 
ref class CDemoProperty : IPickGet,IPickSet
{
internal:
    CDemoProperty(){};
 
protected:
   virtual void PickGetType(){};
 
protected public:
 virtual void PickGetThing(){};
 
public:
  virtual int GetPick() { return this->field; }
    virtual void SetPick(int value) { this->field = value; }
 
private:
    int field;
};
    
ref class CDemoObject sealed : public CDemoProperty
{
public:
 CDemoObject(){};
    /***********************************************
    *          Uncomment to examine the override
    ***********************************************/
    /*virtual void PickGetName() override
   {
       CDemoProperty::PickGetName();
   };*/
 
protected:
 virtual void PickGetType() override
 {
       CDemoProperty::PickGetType();
   };
};
 
void PickThing()
{
  CDemoObject^ cPick = ref new CDemoObject();
 /***********************************************
    * cPick will be able to access the protected  * public member
   ***********************************************/
    cPick->PickGetThing();
   /**********************************************
 * Uncomment the following item to force an error
    * Reason? PickGetType is sealed  **********************************************/
    //cPick->PickGetType();
}
 
int main(Platform::Array<Platform::String^>^ args)
{
   PickThing();
    return 0;
}
/****** End of code *****************************/