Consider the following class written in c# .net 4.0 (typically found in a nhibernate class):
public class CandidateEntity : EntityBase
{
public virtual IList<GradeEntity> Grades { get; set; }
public CandidateEntity()
{
Grades = new List<GradeEntity>();
}
}
This line gets a well founded warning "virtual member call in the constructor". Where shall I initialize this collection ?
Regards,
Which is the correct syntax of declaring a virtual function? Explanation: To make a function virtual function we just need to add virtual keyword at the starting of the function declaration.
A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
In other words, the member function of Base is not overridden. In order to avoid this, we declare the print() function of the Base class as virtual by using the virtual keyword. class Base { public: virtual void print() { // code } }; Virtual functions are an integral part of polymorphism in C++.
Virtual member functions are declared with the keyword virtual . They allow dynamic binding of member functions. Because all virtual functions must be member functions, virtual member functions are simply called virtual functions.
The backing field is one way. Another is to use a private setter. This works well in nHibernate.
public virtual IList<GradeEntity> Grades { get; private set; }
public CandidateEntity()
{
Grades = new List<GradeEntity>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With