Pure Virtual Constructor

Discussion in 'C++' started by Thomas Kowalski, Jan 25, 2007.

  1. Hi everyone,
    is there a way to enforce that every class that inherit from BASE have
    to implement a given constructor?
    Example:

    class Base {
    virtual Base (int,int) = 0;
    }

    class INHERIT : public Base {
    virtual INHERIT(int, int);
    }

    guess the only way is to have something like a protected init method
    that should be called in the constructor??

    Regards,
    T.Kowalski
     
    Thomas Kowalski, Jan 25, 2007
    #1
    1. Advertising

  2. Thomas Kowalski

    mlimber Guest

    On Jan 25, 9:47 am, "Thomas Kowalski" <> wrote:
    > is there a way to enforce that every class that inherit from BASE have
    > to implement a given constructor?
    > Example:
    >
    > class Base {
    > virtual Base (int,int) = 0;
    > }
    >
    > class INHERIT : public Base {
    > virtual INHERIT(int, int);
    > }
    >
    > guess the only way is to have something like a protected init method
    > that should be called in the constructor??


    First, your constructor is private, so only friends or member functions
    (but not derived classes) can access it.

    Second, you can't call a virtual init function from your base class
    constructor
    (http://www.parashift.com/c -faq-lite/ctors.html#faq-10.7).

    Third, to finally answer your question, no. But there may be other ways
    to accomplish your desired end (e.g.,
    http://www.parashift.com/c -faq-lite/virtual-functions.html#faq-20.8).
    What is it you really want to accomplish?

    Cheers! --M
     
    mlimber, Jan 25, 2007
    #2
    1. Advertising

  3. Thomas Kowalski wrote:
    > is there a way to enforce that every class that inherit from BASE have
    > to implement a given constructor?


    No. Why would you want to do that?

    > Example:
    >
    > class Base {
    > virtual Base (int,int) = 0;


    Constructors cannot be virtual (and hence cannot be pure).

    > }

    ;
    >
    > class INHERIT : public Base {
    > virtual INHERIT(int, int);
    > }

    ;

    > guess the only way is to have something like a protected init method
    > that should be called in the constructor??


    Why? If 'Base's constructor requires two arguments, the 'INHERIT's
    constructor *has to* construct the base class subobject by passing
    two integers. Why does 'INHERIT's constructor have to have the same
    arguments?

    I think you misunderstand how constructors are used and what they are
    for.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 25, 2007
    #3
  4. Thomas Kowalski a écrit :
    > Hi everyone,
    > is there a way to enforce that every class that inherit from BASE have
    > to implement a given constructor?
    > Example:
    >


    No. 12.1/4 of the standard.

    > guess the only way is to have something like a protected init method
    > that should be called in the constructor??


    Yes. But from 10.3/6 of the standard this is undefined.
    The reason is that when the virtual function is called, the derived
    object doesn't already exists and cannot use member data.

    Michael
     
    Michael DOUBEZ, Jan 25, 2007
    #4
  5. Thomas Kowalski

    Pete Becker Guest

    Michael DOUBEZ wrote:
    > Thomas Kowalski a écrit :
    >
    >> guess the only way is to have something like a protected init method
    >> that should be called in the constructor??

    >
    > Yes. But from 10.3/6 of the standard this is undefined.
    > The reason is that when the virtual function is called, the derived
    > object doesn't already exists and cannot use member data.
    >


    I'm not sure what it is that you think is undefined, nor how the note in
    10.3/6 relates to this question.

    Calling a virtual function from a constructor is well-defined, but
    typically isn't particularly useful. It calls the version of the
    function for the class that's currently being constructed, not the one
    in the derived class.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Jan 25, 2007
    #5
  6. Thomas Kowalski

    Grizlyk Guest

    Thomas Kowalski wrote:
    > class Base {
    > virtual Base (int,int) = 0;
    >
    > };


    Constructor of any calss is _always_ like virtual. The keyword virtual
    means "use class of real object". When you are creating object, you
    must explicit write name of concrete class of object to create.

    If you need to create object of unknown derived class, you must to
    define ordinary member inside any class, to create concrete class at
    runtime.

    class Base{};
    class Derived1: public Base{};
    class Derived2: public Base{};

    class Factory
    {
    public:
    Base* create();
    };

    Member Factory::create() will encapsulate knowledge how to create
    concrete derived class from Base, for example:

    Base* Factory::create(){ return new Derived2; }

    Class Factory can remove implementation of Factory::create() to derived
    from Factory, in the case Factory::create() must be virtual

    class Factory
    {
    public:
    virtual Base* create()=0;
    };
     
    Grizlyk, Jan 26, 2007
    #6
  7. Thomas Kowalski

    Noah Roberts Guest

    On Jan 25, 7:30 am, Michael DOUBEZ <> wrote:
    > Thomas Kowalski a écrit :


    > > guess the only way is to have something like a protected init method
    > > that should be called in the constructor??

    > Yes. But from 10.3/6 of the standard this is undefined.
    > The reason is that when the virtual function is called, the derived
    > object doesn't already exists and cannot use member data.


    The yes is a bit misleading though. It never works right. IMO
    anything undefined is considered, "no, you can't do that," especially
    when the usual result is anything but what you think you want.
     
    Noah Roberts, Jan 26, 2007
    #7
  8. Thomas Kowalski

    Pete Becker Guest

    Noah Roberts wrote:
    >
    > On Jan 25, 7:30 am, Michael DOUBEZ <> wrote:
    >> Thomas Kowalski a écrit :

    >
    >>> guess the only way is to have something like a protected init method
    >>> that should be called in the constructor??

    >> Yes. But from 10.3/6 of the standard this is undefined.
    >> The reason is that when the virtual function is called, the derived
    >> object doesn't already exists and cannot use member data.

    >
    > The yes is a bit misleading though. It never works right. IMO
    > anything undefined is considered, "no, you can't do that," especially
    > when the usual result is anything but what you think you want.
    >


    But the "It never works right" is also a bit misleading <g>, since the
    effect of calling a virtual function from a constructor is well defined,
    unless the function is pure virtual. On the other hand, it won't solve
    the original problem, because the function belonging to the base
    currently being constructed is called, and not the function belonging to
    the derived class.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Jan 26, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IK
    Replies:
    2
    Views:
    621
    hemraj
    Jul 23, 2004
  2. vsgdp
    Replies:
    7
    Views:
    410
    Ron Natalie
    Sep 25, 2005
  3. Replies:
    10
    Views:
    730
    Richard Herring
    Oct 18, 2005
  4. John Goche
    Replies:
    10
    Views:
    770
    Marcus Kwok
    Dec 8, 2006
  5. Replies:
    7
    Views:
    597
    James Kanze
    May 2, 2007
Loading...

Share This Page