0
votes

I have this class:

ref class Wrapper {
protected:
            Native *pn;

public:
            Wrapper(int val) { pn = new Native( val ); }

};

Then I derive from it:

ref class DerivedWrapper : public Wrapper{
public: 
    DerivedWrapper(int val) {pn = new DerivedNative(val); }
}

The compiler complains: error C2248 ‘ Wrapper::pn’ : cannot access private member declared in class ‘Wrapper’

The base class native pointer is clearly protected, and the derived class should have ready access to it. All my instincts tell me this should work. Is there something peculiar to ref classes?

I’m compiling with VS 2008 SP 1

2
Are the two classes in the same assembly? Otherwise, exporting the type of Native from the base class Assembly is not (directly) allowed and the compiler will implicitly change it to private. But you would get a warning if that happens. - PMF
why not call base constructor in DerivedWrapper constructor to init that field? - Dejan
you need public ref class - user2249683

2 Answers

2
votes

The native type in your case has to have public or protected accessibility in the compiled assembly itself. There is a special make_public pragma directive that can promote the native type. Add this to the code:

#pragma make_public(Native)

The make_public pragma is documented at http://msdn.microsoft.com/en-us/library/ms235607.aspx

0
votes

Consider initializing pn in a constructor of Wrapper, or you need to provide default constructor for your Wrapper. Don't forget 'public' in the ref class definition.

public ref class Wrapper {
protected:
  Native *pn;
  Wrapper(Native * fpn):pn(fpn)
  {}
public:
  Wrapper(int val) { pn = new Native( val ); }
};

ref class DerivedWrapper : public Wrapper{
public: 
  DerivedWrapper(int val):Wrapper(new DerivedNative(val)){}
};