Error Displaying Base Class Using Class
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers virtual function c++ or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
C++ Using Template
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only c++ override takes a minute: Sign up Why would one create a Base Class object with reference to the Derived Class up vote 4 down vote favorite I was practicing inheritance, using a test program in C# and I found out that c++ multiple inheritance the following statement does not throw an error: BaseClass baseObj = new DerivedClass(); Why is this statement allowed and is there a situation where this statement would be useful to a programmer? Here is my test program: class BaseClass { public void show() { Console.WriteLine("Base Class!"); } } class DerivedClass : BaseClass { public void Display() { Console.WriteLine("Derived Class!"); } } class Result { public static void Main() { BaseClass baseObj = new DerivedClass(); baseObj.show(); } } c# inheritance share|improve
C++ Protected
this question edited Jul 5 '15 at 7:03 poke 128k27199267 asked Jul 5 '15 at 6:56 user3083590 4818 3 "Why is this statement allowed" - because an instance of a derived class can be regarded as an instance of the base class. That's the whole basis for polymorphism in C#. –Jon Skeet Jul 5 '15 at 7:01 So, they are useful only when paired with Virtual methods(in base class) and Overriding (in Derived class) ? –user3083590 Jul 5 '15 at 7:08 1 Not really. They're useful any time you want to indicate that all your code is able to operate using only the base class members, so the choice about which derived class to use is localized to the single place where you instantiate it. –Jon Skeet Jul 5 '15 at 7:12 See also: stackoverflow.com/questions/383947 –Jon Skeet Jul 5 '15 at 7:13 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted I recommend you read about inheritance and Polymorphism in more detail. (here and here) In this answer I try to keep concepts simple enough. Why is this statement allowed and is there a situation where this statement would be useful to a programmer? But in order to explain your question a bit lets take a look at simple and classic example of object oriented program that needs to use polymorphism. Assume you are writing a program tha
here for a quick overview of the site Help Center Detailed c++ inheritance answers to any questions you might have Meta Discuss the polymorphism workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join http://stackoverflow.com/questions/31227894/why-would-one-create-a-base-class-object-with-reference-to-the-derived-class the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up templates: parent class member variables not visible in inherited class up vote 15 down vote favorite 5 I have the following http://stackoverflow.com/questions/6592512/templates-parent-class-member-variables-not-visible-in-inherited-class 4 files: 1. arrayListType.h: Declare and define arrayListType class as a template 2. unorderedArrayListType.h: Inherited from arrayListType class and Declares and defines unorderedArrayListType as a template. 3. main1.cpp: Test program to test unorderedArrayListType class. 4. Makefile I get a compile error saying when accessing the protected variables of arrayListType in unorderedArrayListType for example: "length not declared in this scope", "list not declared in this scope", where length and list are protected variables in arrayListType class. The following are the codes: arrayListType.h #ifndef H_arrayListType #define H_arrayListType #include
Things LocationTech Long-Term Support PolarSys Science OpenMDM More Community Marketplace Events Planet Eclipse Newsletter Videos Participate Report a Bug Forums Mailing Lists Wiki IRC How to Contribute Working Groups Automotive Internet of Things LocationTech Long-Term Support PolarSys Science OpenMDM Toggle navigation Home Projects Forums Eclipse Community Forums https://www.eclipse.org/forums/index.php/t/1076407/ Forum Search: Search Help Register Login Home Home» Language IDEs» C / https://msdn.microsoft.com/en-us/library/sf985hc5.aspx C++ IDE (CDT)» Error displaying array in base class Show: Today's Messages :: Show Polls :: Message Navigator Error displaying array in base class [message #1728972] Fri, 08 April 2016 18:27 Paul DeRoccoMessages: 17Registered: April 2012 Junior Member Ubuntu 14.04 LTS Mars 4.5.2 CDT 8.8.1 GCC 4.9.3 GDB 7.7.1 When I try to view the contents of an array contained in a error displaying base class, I get something like Error: Failed to execute MI command: -var-create - * "&((*(struct foo*)(this)).bar)" Error message from debugger back end: -var-create: unable to create variable object The expression in quotes looks reasonable, so it doesn't look like Eclipse is asking for the wrong thing. Other members can be viewed okay, including arrays in the derived class. Is it possible that there is a mismatch between CDT and GDB? Or between CDT and GCC? Or between GCC error displaying base and GDB? I built the GCC 4.9.3 compiler myself, but don't know whether GDB came with CDT or Ubuntu. Report message to a moderator Re: Error displaying array in base class [message #1728981 is a reply to message #1728972] Fri, 08 April 2016 20:47 Marc-Andre LaperleMessages: 208Registered: July 2009 Senior Member Do you have a code snippet that you can share? I think it will make it easier to troubleshoot. Report message to a moderator Re: Error displaying array in base class [message #1728989 is a reply to message #1728981] Sat, 09 April 2016 01:30 Paul DeRoccoMessages: 17Registered: April 2012 Junior Member The problem only seems to happen within virtual functions, perhaps only on "this": #include
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. C# Reference C# Keywords Modifiers Modifiers abstract abstract abstract Access Modifiers abstract async const event extern in out override readonly sealed static unsafe virtual volatile TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. abstract (C# Reference) Visual Studio 2015 Other Versions Visual Studio 2013 Visual Studio 2012 Visual Studio 2010 Visual Studio 2008 Visual Studio 2005 Visual Studio .NET 2003 The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.ExampleIn this example, the class Square must provide an implementation of Area because it derives from ShapesClass: C# Copy abstract class ShapesClass { abstract public int Area(); } class Square : ShapesClass { int side = 0; public Square(int n) { side = n; } // Area method is required to avoid // a compile-time error. public override int Area() { return side * side; } static void Main() { Square sq = new Square(12); Console.WriteLine("Area of the square = {0}", sq.Area()); } interface I { void M(); } abstract class C : I { public abstract void M(); } } // Output: Area of the square = 144 Abstract