Virtual Constructors
-
Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)
C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example:
// Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName. class CFactoryBase { public: virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Mircrosoft(); } }; class CFactory_Borland : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Borland(); } };
In this context theCreate
methods are sometimes called virtual constructors. Steve -
Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)
-
Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)
-
Did you hear it as one of the question when being interviewed? S o h a i l K a d i w a l a
-
C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example:
// Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName. class CFactoryBase { public: virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Mircrosoft(); } }; class CFactory_Borland : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Borland(); } };
In this context theCreate
methods are sometimes called virtual constructors. SteveIronic - The one correct answer gets a 1 vote. Well don't take my word for it: see "The C++ Programming Language", Special Edition by Bjarne Stroustrup, Section 12.4.4 on page 323. Steve
-
C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example:
// Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName. class CFactoryBase { public: virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Mircrosoft(); } }; class CFactory_Borland : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Borland(); } };
In this context theCreate
methods are sometimes called virtual constructors. Steve -
Ironic - The one correct answer gets a 1 vote. Well don't take my word for it: see "The C++ Programming Language", Special Edition by Bjarne Stroustrup, Section 12.4.4 on page 323. Steve
You are SO right Stephen, in both of your posts. Virtual Constructors is a design pattern AKA "Factory Method" that also can be read about in the famous book "Design Patterns" written by GoF (Gamma, Helm, Johnson and Vlissides), which has been the "design pattern bible" for more than 10 years. As an example: every time you create a COM object you use a factory AKA Virtual Constructor. Common knowledge? :~ Strange votings again, reminds me of my post two weeks ago in The Lounge[^] I'll put it up as much as I can... -- Rog
It's suppose to be hard, otherwise anybody could do it!
-
Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)
Rinu_Raj wrote:
Do any one know about Virtual Constructors ?
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
I tried to compile the below code get the error...
class a
{
virtual a()
{
}
};error C2633: 'a' : 'inline' is the only legal storage class for constructors - NS -
NS17 wrote:
error C2633: 'a' : 'inline' is the only legal storage class for constructors
Because the constructor cannot be virtual.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example:
// Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName. class CFactoryBase { public: virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Mircrosoft(); } }; class CFactory_Borland : public CFactoryBase { public: virtual CWorkerBase* Create() { return new CWorkerBase_Borland(); } };
In this context theCreate
methods are sometimes called virtual constructors. SteveI think the voter'd have wanted to give a top '1' vote. just like Rank 1. :-D
--[V]--
-
You are SO right Stephen, in both of your posts. Virtual Constructors is a design pattern AKA "Factory Method" that also can be read about in the famous book "Design Patterns" written by GoF (Gamma, Helm, Johnson and Vlissides), which has been the "design pattern bible" for more than 10 years. As an example: every time you create a COM object you use a factory AKA Virtual Constructor. Common knowledge? :~ Strange votings again, reminds me of my post two weeks ago in The Lounge[^] I'll put it up as much as I can... -- Rog
It's suppose to be hard, otherwise anybody could do it!
Roger Stoltz wrote:
Strange votings again, reminds me of my post two weeks ago in The Lounge[^]
:confused::confused::confused: Which post? The link you provided is changing all the time. Nothing among the posts is expanded (selected). It shifts the frame window (Msgs [N] ~ [N+49]) each time when someone post a new post.
Maxwell Chen