C# inheritance broken?

A

Andre Kaufmann

Reflection is no good, because of the amount of time it takes to make
a copy for a large document.

It wasn't my intention to use reflection directly, but to use it to
create new source code, which has implemented all methods of the
Document object, so that you don't have to do the wrapping by "hand".

Andre
 
B

Bruce Wood

Mike said:
[...]
But you haven't shown a C++ solution either, since the cast in your example
doesn't work.

If a constructor is added like in the following example, it will work.
Think that was the intention of Tony.

class Document
{
public:
Document(){}
static Document Load() { return Document(); }

};

class MyDocument : public Document
{
public:
MyDocument() {}
MyDocument(Document& s) : Document(s) {}
static MyDocument Load() { return Document::Load(); }

};

int main(int argc, char* argv[])
{
MyDocument s = MyDocument::Load();
return 0;

}

Aren't you forgetting something? Where is the copy constructor for
Document that accepts a pointer to a Document and creates a new
document from it? (Invoked on the line:

MyDocument(Document& s) : Document(s) {}

) Or is this copy constructor somehow automatically generated by the C
++ compiler?
 
A

Andre Kaufmann

Bruce said:
[...]
MyDocument(Document& s) : Document(s) {}

) Or is this copy constructor somehow automatically generated by the C
++ compiler?


Exactly. C++ will automatically generate a copy constructor on a binary
basis (memcopy) or will call a user defined one for each of the class
variables. However, a developer can make the copy constructor private in
a class and then you surely can't construct an object as I did in the
example.

Andre
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Something similar can be implemented in the base class and would be
inherited by your class. There may be a performance issue with
Reflection though.

In C++ I remember this idea has a name, a copycon constrctor?

What strike me as weird is what is the problem in handling a Docment in the
first place, IMO MyDocument extend Document but the application should be
made to handle Document as default. And finally the readed file IS A
document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top