Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
599 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

class CDerived : CAbstract
{
     public CDerived() : base("some default argument") { }
     public CDerived(string arg) : base(arg) { }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...