derived

C# - Can publicly inherited methods be hidden (e.g. made private to derived class)

萝らか妹 提交于 2019-11-27 18:40:55
Suppose I have BaseClass with public methods A and B, and I create DerivedClass through inheritance. e.g. public DerivedClass : BaseClass {} Now I want to develop a method C in DerivedClass that uses A and B. Is there a way I can override methods A and B to be private in DerivedClass so that only method C is exposed to someone who wants to use my DerivedClass? Brian R. Bondy It's not possible, why? In C#, it is forced upon you that if you inherit public methods, you must make them public. Otherwise they expect you not to derive from the class in the first place. Instead of using the is-a

C# XML serialization of derived classes

一个人想着一个人 提交于 2019-11-27 15:29:21
Hi I am trying to serialize an array of objects which are derived from a class and I keep hitting the same error using c#. Any help is much appreciated. obviously this example has been scaled down for the purpose of this post in the real world Shape would contain a plethora of different shapes. Program.cs namespace XMLInheritTests { class Program { static void Main(string[] args) { Shape[] a = new Shape[1] { new Square(1) }; FileStream fS = new FileStream("C:\\shape.xml", FileMode.OpenOrCreate); XmlSerializer xS = new XmlSerializer(a.GetType()); Console.WriteLine("writing"); try { xS.Serialize

Use of “Public” in a derived class declaration?

徘徊边缘 提交于 2019-11-27 11:12:49
问题 Given this base class: class Employee { char* name; int age; public: Employee(char* name); void print(); }; With regards to the "public", what's the difference between this: class Manager : public Employee { EmployeeList employees; public: Manager(char* name, Employee* people); void print(); }; and this: class Manager : Employee { EmployeeList employees; public: Manager(char* name, Employee* people); void print(); }; 回答1: The default is private inheritance. take this example: class B { };

Why would the conversion between derived* to base* fails with private inheritance?

一笑奈何 提交于 2019-11-27 01:50:01
问题 Here is my code - #include<iostream> using namespace std; class base { public: void sid() { } }; class derived : private base { public: void sid() { } }; int main() { base * ptr; ptr = new derived; // error: 'base' is an inaccessible base of 'derived' ptr->sid(); return 0; } This gives a compile time error. error: 'base' is an inaccessible base of 'derived' Since the compiler will try and call the base class sid() why do I get this error? Can someone please explain this. 回答1: $11.2/4 states-

C# - Can publicly inherited methods be hidden (e.g. made private to derived class)

吃可爱长大的小学妹 提交于 2019-11-26 22:42:39
问题 Suppose I have BaseClass with public methods A and B, and I create DerivedClass through inheritance. e.g. public DerivedClass : BaseClass {} Now I want to develop a method C in DerivedClass that uses A and B. Is there a way I can override methods A and B to be private in DerivedClass so that only method C is exposed to someone who wants to use my DerivedClass? 回答1: It's not possible, why? In C#, it is forced upon you that if you inherit public methods, you must make them public. Otherwise

C# XML serialization of derived classes

天大地大妈咪最大 提交于 2019-11-26 18:28:26
问题 Hi I am trying to serialize an array of objects which are derived from a class and I keep hitting the same error using c#. Any help is much appreciated. obviously this example has been scaled down for the purpose of this post in the real world Shape would contain a plethora of different shapes. Program.cs namespace XMLInheritTests { class Program { static void Main(string[] args) { Shape[] a = new Shape[1] { new Square(1) }; FileStream fS = new FileStream("C:\\shape.xml", FileMode

How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++?

荒凉一梦 提交于 2019-11-26 09:45:35
I'm using a vector of pointers to objects. These objects are derived from a base class, and are being dynamically allocated and stored. For example, I have something like: vector<Enemy*> Enemies; and I'll be deriving from the Enemy class and then dynamically allocating memory for the derived class, like this: enemies.push_back(new Monster()); What are things I need to be aware of to avoid memory leaks and other problems? GManNickG std::vector will manage the memory for you, like always, but this memory will be of pointers, not objects. What this means is that your classes will be lost in

How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++?

╄→гoц情女王★ 提交于 2019-11-26 02:05:14
问题 I\'m using a vector of pointers to objects. These objects are derived from a base class, and are being dynamically allocated and stored. For example, I have something like: vector<Enemy*> Enemies; and I\'ll be deriving from the Enemy class and then dynamically allocating memory for the derived class, like this: enemies.push_back(new Monster()); What are things I need to be aware of to avoid memory leaks and other problems? 回答1: std::vector will manage the memory for you, like always, but this