abstract class CAbstract
{
private string mParam1;
public CAbstract(string param1)
{
mParam1 = param1;
}
}
class CBase : CAbstract
{
}
For the class CBase, it should be initialized by providing the parameter, so how to disable the parameterless constructor for CBase class?
If you define a parameterized constructor in
CBase
, there is no default constructor. You do not need to do anything special.If your intention is for all derived classes of
CAbstract
to implement a parameterized constructor, that is not something you can (cleanly) accomplish. The derived types have freedom to provide their own members, including constructor overloads.The only thing required of them is that if
CAbstract
only exposes a parameterized constructor, the constructors of derived types must invoke it directly.To disable default constructor you need to provide non-default constructor.
The code that you pasted is not compilable. To make it compilable you could do something like this:
Please correct me if I am wrong, but I think I achieved that goal with this code: