On Feb 12, 2009, at 2:31 AM, João Varela wrote:

João,

Hi all

I am porting most of my C++ code into Objective C and at the moment I have this problem:

In C++ to force a constructor to call a overriding method of a subclass I used pure virtual functions defined in an abstract (super) class.

hopefully it's not too late already, but I have to say that your code in C++ will not work as you expect. Actually, if your virtual function in the base class is pure, you (should) get a linker error.

Fortunately, the behavior is well defined in the Standard. For more information please read:
http://www.research.att.com/~bs/bs_faq2.html#vcall

or read "polymorphic behavior of an object under construction" in the C ++ Standard:

Member functions, including virtual functions, can be called during construction or destruction. When a virtual function is called directly or indirectly from a constructor, and the object to which the call applies is the object under construction, the function called is the one defined in the constructor's own class or in one of its bases, but not a function overriding it in a class derived from the constructor's class.


Regards
Andreas



An example:

class Foo
{
        Foo();
        virtual read( x, y ) = 0; // pure virtual
};

class Bar : public Foo
{
        Bar();
        
        virtual Read( x, y );   // implemented in the subclass
};

// Foo constructor
Foo::Foo()
{
        Read( x, y ); // <-- this calls Bar::Read and not Foo::Read
                      // because the latter is pure virtual
}

I know that the closest thing to a pure virtual function in Objective C is a formal protocol. My question is: can you implement such a behavior in objective C with a formal protocol?

Related to this, what method gets called inside the init method of the superclass: a) the superclass method? or b) the overriding method if there is one?

Although this is not strict Cocoa code, I think this is the best place to ask this kind of question. Sorry for the mild "OT-ness" of my question.

TIA

JV



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/agrosam%40onlinehome.de

This email sent to agro...@onlinehome.de

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to