hi,
i derive my class from the abstract Stream class. my class contains static members. one of them makes use of a static variable member, where an error is generated at compile time (C2259: cannot instantiate abstract class). of course this is due to the fact, that the static instance of the class is created at the first time, a static member is accessed.
why is the restriction, that no objects can be created from abstract classes, also true for the static instance is there a way of get around this
thanks!

Class derived from abstract class with static members
nature0276
i defined Dispose(bool) only because i wanted to override all abstract and virtual members to avoid error C2605. so the topic is still quite complex...
at this point i could of course create a class inside D2XX which derives from Stream to keep all static definitions out of the class. but in my eyes this is a bad-style implementation of my library, i'm currently working on.
xRuntime
now the code compiles successfully!
by the way the types of e.g. the Write method didn't match with the ones of the base class: Int64 should be used instead of long. after that compilation succeeds ;-)
Dave Patricio
" subsequently i tried it with implementing ALL members (virtual and abstract from the Stream class):"
No, you did not, you have just created "new" members. You need to use virtual/override keywords to implement the abstract Stream members. You also need to implement all the abstract members (you missed Position, Length for example). Also you use long for some functions like Seek but you really should be using Int64 or long long.
virtual void Write(array<Byte>^ buffer, int offset, int count) override{
}
virtual int Read(array<Byte>^ buffer, int offset, int count) override
{
return 0;
}
virtual void Flush() override
{
}
virtual Int64 Seek(Int64 offset, SeekOrigin origin) override
{
return 0;
}
virtual void SetLength(Int64 value) override
{
}
property Int64 Position
{
virtual Int64 get() override { return 0; }
virtual void set(Int64 value) override { }
}
property Int64 Length
{
virtual Int64 get() override { return 0; }
}
property bool CanRead
{
virtual bool get() override { return true; }
}
property bool CanWrite
{
virtual bool get() override { return true; }
}
property bool CanSeek
{
virtual bool get() override { return false; }
}
SvenC is correct about the Dispose function. You need to add a destructor instead of a Dispose member. In addition there is not much need to override Close because it's not abstract and all it does is calling Dipose(true) which in turn will call your destructor. The Dispose member is automatically generated by the compiler.
oris
thanks for your reply!
the compiler stops at the red marked line ("error C2259: 'D2XX': cannot instantiate abstract class"):
public ref class D2XX : public Stream
{
protected: D2XX(...)
: Stream()
{
...
}
...
public: static List<D2XX^>^ _Devices = gcnew List<D2XX^>^();
...
public: static property array<D2XX^>^ Devices
{
array<D2XX^>^ get()
{
...
if (!FoundInList)
_Devices->Add(gcnew D2XX(pCDeviceItem));
}
}
can you find anything erroneous
thanks!
fel lobo
So could you try to use ~D2XX() and !D2XX() instead of Dispose(bool) Just follow the pattern in the link I provided. I really think it is just the compiler "protecting" the name Dispose because it generates code automatically that creates the Dispose pattern automatically.
--
SvenC
Kishorepbp
This compiles fine. Where is your difference:
// cppCLR.cpp : main project file.
#include
"stdafx.h"using
namespace System;ref
class ABC abstract : public System::IO::Stream{
public:
static int i = 0;
static int f()
{
return i;
}
};
int
main(array<System::String ^> ^args){
Console::WriteLine(L"Hello World" + ABC::f().ToString());
return 0;
}
--
SvenC
VenkatRaghavan
public : void Write(array<Byte>^ buffer, int offset, int count) new
{
}
public : int Read(array<Byte>^ buffer, int offset, int count) new
{
return 0;
}
public : void Close() new
{
}
public : void Flush() new
{
}
public : long Seek(long offset, SeekOrigin origin) new
{
return 0;
}
public : void SetLength(long value) new
{
}
public : property bool CanRead
{
bool get() new { return true; }
}
public : property bool CanWrite
{
bool get() new { return true; }
}
public : property bool CanSeek
{
bool get() new { return false; }
}
public : property bool CanTimeout
{
bool get() new { return false; }
}
the only member i could not overwrite was Dispose(bool):
protected : void Dispose(bool disposing)
{
}
but then i get the error message:
error C2605: ''Dispose'' : this method is reserved within a managed class.
the gcnew D2XX(...) command of course creates an instance of D2XX and thus of Stream, but when the base classes' abstract members are all overridden, this should not pose a big problem. or not
Ted.
Well that code does not use static members, it creates an instance of D2XX - that accesses the instance constructor. So you have to implement all abstract base members.
--
SvenC
nzmike
Well, I answered your first post and there you talked about static member access causing the error about abstract class usage.
The last error is a bit strange. As I could see D2XX is a ref class, so it is managed and still it says to use Dispose only in managed classes
Can you show all Dispose methods you have in D2XX and their accessibility (private, public...)
As an alternative or maybe the correct way IIRC you might want to implement ~D2XX() and !D2XX() when you need finalization instead of Dispose. I think the compiler will then generate the Dispose code automatically. Check this msdn ref: http://msdn2.microsoft.com/en-us/library/ms177197.aspx
--
SvenC