locked
(newbie) How to initialize a private static member..

    Question

  • I'm trying to play with WinRT/C++ (I'm new to C++ but WinRT / XAML C++ UI are convincing me to give C++ another go!)

     

    At any rate, I'm trying to write some helper code to implement ICustomPropertyProvider.

    I have a class implementing ICustomPropertyProvider that can be build programatically and I'm trying to have a static instance member of it to have one members map per class.

     

    Something like that:

    #pragma once
    #include "pch.h"
    
    using namespace Windows::Foundation;
    using namespace Experiments::MetaInfo;
    
    namespace Experiments
    {
        public delegate void SomeEventHandler(int i);
    
    	public ref class Model sealed : public ViewModelBase, public ICustomPropertyProvider, public IDisposable
        {
    	private:
    		ref class ModelPropertyProvider : PropertyProvider
    		{
    			ModelPropertyProvider()
    			{
    				//AddCustomProperty(
    				//	ref new PropertyInfo(
    				//		"PropertyA"
    				//		, "int"
    				//		, ref new PropertyGetter([](Object^ instance) {return PropertyValue::CreateInt32(((WinRTComponent^)instance)->PropertyA);})
    				//		, ref new PropertySetter([](Object^ instance, Object^ value) {((WinRTComponent^)instance)->PropertyA = ((PropertyValue^)value)->GetInt32();})
    				//	)
    				//);
    			}
    		};
    		<strong>static ModelPropertyProvider myPProvider</strong>;
    		// this macro implement ICustomPropertyProvider
    		PROPERTY_PROVIDER_IMPL("Model", myPProvider);
    	public:
    
    	private:
            int mPropertyA;
    
    	protected:
    
        public:
            Model ()
    		{
    			mPropertyA = 23;
    		}
    		~Model () {}
    
            property int PropertyA
            {
                int get() 
    			{
    				return mPropertyA; 
    			}
                void set(int propertyAValue) 
    			{
    				if (propertyAValue == mPropertyA)
    					return;
    				mPropertyA = propertyAValue; 
    				FirePropertyChanged("PropertyA");
    			}
            }
    
    		property String^ PropertyS
            {
                String^ get() { return "Hello"; }
                void set(String^ p) 
    			{
    			}
            }
    
    		//int Method(int i) { return i; }
    
            //event SomeEventHandler^ someEvent;
    
        };
    }
    // Model.h
    
    


    But when I try to compile and use this class in my project I got error along the lines:

    Error 1 error LNK2005: "private: static class Experiments::Model::ModelPropertyProvider Experiments::Model::myPProvider" (?myPProvider@Model@Experiments@@0VModelPropertyProvider@12@A) already defined in MainPage.xaml.obj C:\Dev\Tests\Mix2\Experiments\Model.obj Experiments

    I have been poking at the code for 1 hour now, I didn't find a way to initialize my private static member variable (of a type of a private class). (make it a pointer or not)
    Any tip on how I can this thing to compile??
    Thanks! :)

    Saturday, January 21, 2012 12:46 PM

Answers

  • Well I manage to get around that by having a private code with non CX class, worked well.. 

    I guess the C++ compiler is not up to all C++ tricks with CX classes yet!!!

    • Marked as answer by Lloyd Sunday, January 22, 2012 2:07 PM
    Sunday, January 22, 2012 2:07 PM