Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual constructor

Reply
Thread Tools

virtual constructor

 
 
uday
Guest
Posts: n/a
 
      11-25-2004
can u explain me the concept of virtual constructor. Does such concept
exists in c++

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-25-2004
"uday" <> wrote...
> can u explain me the concept of virtual constructor. Does such concept
> exists in c++


Read the FAQ.


 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      11-25-2004
> can u explain me the concept of virtual constructor. Does such concept
> exists in c++


That's a design pattern, not a built-in construct.

From the language's POV, the actual virtual function called depends on
the object on which it is called. Having a virtual constructor makes no
sense because there are no objects yet.

Still, it should be possible to create different type of objects
depending on a given object. For exemple, having an object of type
SillyMonsterCreator would create SillyMonster's and an object of type
BadMonsterCreator would create BadMonster's. The Virtual Constructor
pattern is also called Factory.

class Monster
{};

class SillyMonster : public Monster
{};

class BadMonster : public Monster
{};

class MonsterCreator
{
public:
virtual Monster *create() = 0;
};

class SillyMonsterCreator : public MonsterCreator
{
public:
SillyMonster *create() { return new SillyMonster; }
};

class BadMonsterCreator : public MonsterCreator
{
public:
BadMonster *create() { return new BadMonster; }
};


void f(MonsterCreator &creator)
{
// that's our virtual contructor call
Monster *m = creator.create();

// here we have a monster of some type, depending on 'creator'

delete m;
}


int main()
{
SillyMonsterCreator smc;
BadMonsterCreator bmc;

// let`s create a silly monster
f(smc);

// let's create a bad monster
f(bmc);
}


Jonathan
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-25-2004
"Jonathan Mcdougall" <> wrote...
>> can u explain me the concept of virtual constructor. Does such concept
>> exists in c++

>
> That's a [...]


That's a FAQ

http://www.parashift.com/c++-faq-lit...t.html#faq-5.5


 
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
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM
java like constructor calling constructor lallous C++ 5 01-23-2004 11:52 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57