473,623 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Force Implementation of a Constructor

I know it's possible to require inheriting classes to implement a particular
function or property, but is it possible to require a inheriting class to
implement a constructor of it's own?

Thanks,
Steve
Nov 15 '05 #1
21 10172
Steve - DND <ng@digitalnoth ing.com> wrote:
I know it's possible to require inheriting classes to implement a particular
function or property, but is it possible to require a inheriting class to
implement a constructor of it's own?


No, I'm afraid not.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
100
Hi Steve,
No, there is no way?
Unless if you declare only protected constructors for the base class.

class Foo
{
protected Foo()
{
}
}

Now if I want to inherit from this class and be able to instatiate objects I
have to declare at least one public constructor. But I cannot force the
inheritors to declare constructor with specific prototype (as I can do with
abstract methods).

class Bar: Foo
{
public Bar()
{
}
}

What do you need that for? I can't think of any useful application of this.

HTH
B\rgds
100

"Steve - DND" <ng@digitalnoth ing.com> wrote in message
news:uL******** ******@TK2MSFTN GP09.phx.gbl...
I know it's possible to require inheriting classes to implement a particular function or property, but is it possible to require a inheriting class to
implement a constructor of it's own?

Thanks,
Steve

Nov 15 '05 #3
100 <10*@100.com> wrote:

<snip>
What do you need that for? I can't think of any useful application of this.


I can, very easily: plug-in systems. Basically, sooner or later in any
plug-in framework you need to instantiate a type which you don't know
about at compile-time. You'll have to instantiate it with reflection
anyway, but it would help if the plug-in author got an error when
writing the type if they didn't provide an appropriate constructor.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
What do you need that for? I can't think of any useful application of
this. I can, very easily: plug-in systems. Basically, sooner or later in any
plug-in framework you need to instantiate a type which you don't know
about at compile-time. You'll have to instantiate it with reflection
anyway, but it would help if the plug-in author got an error when
writing the type if they didn't provide an appropriate constructor.


Wouldn't you use an interface to define the plugin, then why would you care
about the constructor? What I mean is the functionality of a Foo
constructor Foo(int a, int b); Could simply be Init(int a, int b) in an
interface, ignoring a constructor completely.

-- Alan
Nov 15 '05 #5
You could do this with generics.

But if you need a definatie API, use an interface, or if you need a definate
behaviour, use abstract base classes

"Alan Pretre" <no@spam> wrote in message
news:#k******** ******@TK2MSFTN GP12.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
What do you need that for? I can't think of any useful application of
this.
I can, very easily: plug-in systems. Basically, sooner or later in any
plug-in framework you need to instantiate a type which you don't know
about at compile-time. You'll have to instantiate it with reflection
anyway, but it would help if the plug-in author got an error when
writing the type if they didn't provide an appropriate constructor.


Wouldn't you use an interface to define the plugin, then why would you

care about the constructor? What I mean is the functionality of a Foo
constructor Foo(int a, int b); Could simply be Init(int a, int b) in an
interface, ignoring a constructor completely.

-- Alan

Nov 15 '05 #6
"news.microsoft .com" <di********@dis cussion.microso ft.com> wrote in message
news:OH******** *****@tk2msftng p13.phx.gbl...
But if you need a definatie API, use an interface, or if you need a definate behaviour, use abstract base classes


In this case you could do both. Inherit functionality from PluginBase() and
yet implement IPlugin.

-- Alan
Nov 15 '05 #7
Alan Pretre <no@spam> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
What do you need that for? I can't think of any useful application of
this.
I can, very easily: plug-in systems. Basically, sooner or later in any
plug-in framework you need to instantiate a type which you don't know
about at compile-time. You'll have to instantiate it with reflection
anyway, but it would help if the plug-in author got an error when
writing the type if they didn't provide an appropriate constructor.


Wouldn't you use an interface to define the plugin, then why would you care
about the constructor?


To create an instance of the type that implements the interface.
What I mean is the functionality of a Foo
constructor Foo(int a, int b); Could simply be Init(int a, int b) in an
interface, ignoring a constructor completely.


How do you call Init in the first place? If it's a static method, you
can't specify that in the interface, and if it's an instance method
then you're back to the question of how you create an instance in the
first place.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Wouldn't you use an interface to define the plugin, then why would you care about the constructor?


To create an instance of the type that implements the interface.


By definition, it would be of the type if it implemented the interface.
What I mean is the functionality of a Foo
constructor Foo(int a, int b); Could simply be Init(int a, int b) in an
interface, ignoring a constructor completely.


How do you call Init in the first place? If it's a static method, you
can't specify that in the interface, and if it's an instance method
then you're back to the question of how you create an instance in the
first place.


I mean created with a default constructor then refined based on appropriate
instance methods or overloads or whatever.

-- Alan
Nov 15 '05 #9
Alan Pretre <no@spam> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Wouldn't you use an interface to define the plugin, then why would you
care> about the constructor?


To create an instance of the type that implements the interface.


By definition, it would be of the type if it implemented the interface.


Yes - but you'd need to create an instance of it in order to use it
though, is my point.
How do you call Init in the first place? If it's a static method, you
can't specify that in the interface, and if it's an instance method
then you're back to the question of how you create an instance in the
first place.


I mean created with a default constructor then refined based on appropriate
instance methods or overloads or whatever.


That relies on there *being* a parameterless constructor though - and
you can't enforce that. (It would also restrict the plugin to being a
mutable type, which isn't ideal.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2704
by: Dev | last post by:
Hello, In the following class definition, the ZString destructor is invoked two times. This crashes the code. class ZString { public: ZString(char* p)
9
3668
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; template<class T> classSubject {
3
1924
by: Arran Pearce | last post by:
Hi, If i have a abstract class (e.g. Class1) and then i make Class2 which inherits from Class1. I have a method in Class1 which i want to force Class2 to run at some point. Is there a way i can force this? I dont want to run the method in the constructor of Class1 as the Constructor needs to do some work in Class2 before the method will work correctly. thanks
10
10684
by: Marc Selis | last post by:
Hi, I have a class with a static constructor in which the class registers itself as capable of doing something (using a delegate) The problem is that the static constructor is never called, as no other class references it. The only way to communicate with the class is using the delegate. Here is my code:
18
3115
by: lchian | last post by:
Hi, I have a vector of class Foo. When I do a push_back(), I expect stl to call the default constructor I wrote for Foo. But instead, stl makes up its own default that is initialized with garbage. Is there a way to force stl to use MY default constructor?
1
2801
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw exception. However, how can we make the interface easy to use correctly and hard to use incorrectly? Client may forget/ ignore from having a try...catch block whenever they call the constructor. Is there any way we can prevent this from happen?
6
27792
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if it has arguments. (example ** assuming the Person class's constructor has (string FirstName) as...
7
2057
by: desktop | last post by:
I the C++ standard page 472 it says that an associative container can be constructed like X(i,j,c) where i and j are input iterators to elements. But in the implementation there is no constructor that matches this requirement, the only constructors are: public: // allocation/deallocation _Rb_tree() { }
0
8217
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8661
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6104
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2590
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.