 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri Jun 02, 2006 1:50 am Post subject: enforcing the constructor interface? |
|
|
Maybe I'm missing the point, please tell me;
Wouldn't it be nice if constructor parameters (the interface) could
be specified in abstract base classes just like with other functions?
I find this a bit annoying, when making "pluginable object" interfaces.
There is no way I can make sure that all pluginable objects
constructors take the same parameters.
1. I could make the assumption that all constructors take no
parameters, and then have a regular "init" function that does the
initialization job the constructor should have done. Clients would have
to remember to always call init right after construction. This is not
so good, because that would mean objects could be constructed and used
without being initialized / in valid state.
2. I could make the constructor private and have a regular function
called something like newInstance do the actual construction work. This
is a bit bad because clients would have to create objects in this
non-standard way.
Maybe there is a generic programming solution to enforcing the
constructor interface?
Cheers /Petke
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Eugene Alterman Guest
|
Posted: Sun Jun 04, 2006 5:34 am Post subject: Re: enforcing the constructor interface? |
|
|
<patrik.kahari (AT) sofe (DOT) nu> wrote in message
news:1149152948.603932.238780 (AT) c74g2000cwc (DOT) googlegroups.com...
| Quote: | Maybe I'm missing the point, please tell me;
Wouldn't it be nice if constructor parameters (the interface) could
be specified in abstract base classes just like with other functions?
I find this a bit annoying, when making "pluginable object" interfaces.
There is no way I can make sure that all pluginable objects
constructors take the same parameters.
1. I could make the assumption that all constructors take no
parameters, and then have a regular "init" function that does the
initialization job the constructor should have done. Clients would have
to remember to always call init right after construction. This is not
so good, because that would mean objects could be constructed and used
without being initialized / in valid state.
2. I could make the constructor private and have a regular function
called something like newInstance do the actual construction work. This
is a bit bad because clients would have to create objects in this
non-standard way.
Maybe there is a generic programming solution to enforcing the
constructor interface?
|
Use the Abstract Factory design pattern.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
petke Guest
|
Posted: Sat Jun 10, 2006 4:39 am Post subject: Re: enforcing the constructor interface? |
|
|
Thanks for your reply, that made sense.
| Quote: | There are no pointers to constructors in C++.
|
I had no idea. Why is this so?
cheers /Petke
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Sun Jun 11, 2006 12:48 am Post subject: Re: enforcing the constructor interface? |
|
|
"petke" <patrik.kahari (AT) sofe (DOT) nu> skrev i meddelandet
news:1149860777.227079.177720 (AT) f6g2000cwb (DOT) googlegroups.com...
| Quote: | Thanks for your reply, that made sense.
There are no pointers to constructors in C++.
I had no idea. Why is this so?
|
They are not explicitly banned, it is just that there is no way of
forming a pointer to a constructor.
If you were to try it, you would run into things like:
- what is the type of the pointer?
- constructors don't have unique names, they share the class name.
What do you take the address of?
something* pointer_to_constructor = &what_goes_here; // ??
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|