Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie questions about constructors and destructors

Reply
Thread Tools

Newbie questions about constructors and destructors

 
 
sieg1974
Guest
Posts: n/a
 
      04-23-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-23-2004
"sieg1974" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Oystein Haare
Guest
Posts: n/a
 
      04-23-2004
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-lit....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


 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      04-23-2004

"sieg1974" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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();
}


 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      04-23-2004

"jeffc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "sieg1974" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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.)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual Constructors and Destructors Alok C++ 5 04-16-2006 05:32 PM
problem in calling constructors and destructors rahul8143@gmail.com C++ 3 08-05-2005 01:19 PM
Pure virtual methods, constructors and destructors Ruben Campos C++ 5 04-06-2005 11:51 PM
How do the STL containers interact with destructors/constructors? velthuijsen C++ 3 02-13-2004 02:52 PM
Can we have private constructors and destructors Rajesh Garg C++ 3 07-22-2003 10:13 PM



Advertisments