Newbie questions about constructors and destructors

Discussion in 'C++' started by sieg1974, Apr 23, 2004.

  1. sieg1974

    sieg1974 Guest

    Hi,

    I'm learning how to program in C++, and I came up with a couple of
    questions.

    1. Can I declare a constructor or destructor virtual? If so, please
    provide me an example where I could use it.

    2. Could a constructor be declared private? If so, how could I
    instantiate an object of this class? I think I could use friend
    methods to do it, but I'm not sure.

    Thanks in advance,

    Andre
     
    sieg1974, Apr 23, 2004
    #1
    1. Advertising

  2. "sieg1974" <> wrote...
    > I'm learning how to program in C++, and I came up with a couple of
    > questions.
    >
    > 1. Can I declare a constructor or destructor virtual? If so, please
    > provide me an example where I could use it.


    A constructor cannot be virtual. A destructor can. Please read your
    favourite C++ book about virtual destructors. Also, it is covered in
    the FAQ (http://www.parashift.com/c -faq-lite/).

    > 2. Could a constructor be declared private? If so, how could I
    > instantiate an object of this class? I think I could use friend
    > methods to do it, but I'm not sure.


    Yes, it can. You would leave instantiation of such class to the
    class members or to the friends as you stated. Read about "named
    constructor idiom" (or "the factory method"). This also is covered
    in the FAQ.

    Victor
     
    Victor Bazarov, Apr 23, 2004
    #2
    1. Advertising

  3. On Thu, 22 Apr 2004 20:39:09 -0700, sieg1974 wrote:

    > Hi,
    >
    > I'm learning how to program in C++, and I came up with a couple of
    > questions.
    >
    > 1. Can I declare a constructor or destructor virtual? If so, please
    > provide me an example where I could use it.


    Constructor: No! (At least no directly)
    Destructor: Yes. Do it whenever it is likely that someone will inherit
    your class.
    http://www.parashift.com/c -faq-lite/virtual-functions.html#faq-20.5

    >
    > 2. Could a constructor be declared private? If so, how could I
    > instantiate an object of this class? I think I could use friend methods
    > to do it, but I'm not sure.


    Yes it can, for example when using the singleton design pattern: you have
    a static function that returns the one and only instance.

    >
    > Thanks in advance,
    >
    > Andre
     
    Oystein Haare, Apr 23, 2004
    #3
  4. sieg1974

    jeffc Guest

    "sieg1974" <> wrote in message
    news:...
    > Hi,
    >
    > I'm learning how to program in C++, and I came up with a couple of
    > questions.
    >
    > 1. Can I declare a constructor or destructor virtual?


    No.

    > 2. Could a constructor be declared private?


    Yes.

    > If so, how could I
    > instantiate an object of this class? I think I could use friend
    > methods to do it, but I'm not sure.


    class A
    {
    public:
    static A* createA() { return new A; }
    private:
    A() {}
    };
    int main()
    {
    A* pA = A::createA();
    }
     
    jeffc, Apr 23, 2004
    #4
  5. sieg1974

    jeffc Guest

    "jeffc" <> wrote in message
    news:...
    >
    > "sieg1974" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I'm learning how to program in C++, and I came up with a couple of
    > > questions.
    > >
    > > 1. Can I declare a constructor or destructor virtual?

    >
    > No.


    Read too fast. The others who said a destructor can be virtual are correct
    (when using polymorphism destructors should be virutal.)
     
    jeffc, Apr 23, 2004
    #5
    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. Rajesh Garg
    Replies:
    3
    Views:
    3,526
    Victor Bazarov
    Jul 22, 2003
  2. velthuijsen
    Replies:
    3
    Views:
    5,326
    velthuijsen
    Feb 13, 2004
  3. Ruben Campos
    Replies:
    5
    Views:
    4,190
    Ron Natalie
    Apr 7, 2005
  4. Replies:
    3
    Views:
    315
    Chris Theis
    Aug 5, 2005
  5. Alok
    Replies:
    5
    Views:
    4,824
    ryadav
    Apr 16, 2006
Loading...

Share This Page